首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring引文注入

2012-11-05 
spring注解注入beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.o

spring注解注入

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
??? xmlns:context="http://www.springframework.org/schema/context"
??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
??? default-lazy-init="true">
??? <description>Spring公共配置文件</description>

??? <!-- 定义受环境影响易变的变量 -->
??? <bean value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
??? ??? <property name="ignoreResourceNotFound" value="true" />
??? ??? <property name="locations">
??? ??? ??? <list>
??? ??? ??? ??? <value>classpath:application.properties</value>
??? ??? ??? </list>
??? ??? </property>
??? </bean>

??? <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
???

??? <!-- 数据源配置,在开发测试环境直连数据库 -->
??? <bean id="dataSource" value="com.mysql.jdbc.Driver" />
??? ??? <property name="url" value="jdbc:mysql://localhost:3306/mshop" />
??? ??? <property name="username" value="root" />
??? ??? <property name="password" value="mysql" />
??? </bean>


??? <!-- Hibernate配置 -->
??? <bean id="sessionFactory" ref="dataSource" />
??? ??? <property name="namingStrategy">
??? ??? ??? <bean />
??? ??? </property>
??? ??? <property name="hibernateProperties">
??? ??? ??? <props>
??? ??? ??? ??? <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
??? ??? ??? ??? <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
??? ??? ??? ??? <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
??? ??? ??? </props>
??? ??? </property>
??? ??? <property name="packagesToScan" value="com.jweb.entity.*" />
??? </bean>

??? <!-- 事务管理器配置,单数据源事务 -->
??? <bean id="transactionManager" ref="sessionFactory" />
??? </bean>
???

</beans>

这一步关键是就是红色标识的地方。

第二步:在service及dao上使用注解说明这个组件是service还是dao

package com.jweb.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jweb.dao.user.UserDao;
import com.jweb.entity.user.User;

@Service
@Transactional
public class UserService {

?? @Autowired
??? private UserDao mdao;

????
??? public UserDao getMdao() {
??? ??? return mdao;
??? }


??? public void setMdao(UserDao mdao) {
??? ??? this.mdao = mdao;
??? }


??? public List<User> findAll(){
??? ??? return mdao.findAll();
??? }
????
}

package com.jweb.dao.user;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jweb.entity.user.User;

@Repository
public class UserDao {
??? private SessionFactory sessionFactory;

??? public SessionFactory getSessionFactory() {
??? ??? return sessionFactory;
??? }

??? @Autowired
??? public void setSessionFactory(SessionFactory sessionFactory) {
??? ??? this.sessionFactory = sessionFactory;
??? }
????
??? @SuppressWarnings("unchecked")
??? public List<User> findAll(){
??? ??? return sessionFactory.getCurrentSession().createQuery("from User").list();
??? }
}

用@service和@Repository只是明确的标识这个是什么类型的类,当然也可以用@Component来标识这个是一个可以注入的组件类。
这样就不在需要在xml文件中配置大量的bean了,是比较方便。

热点排行