一、spring配置文件(applicationContext.xml):
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源 -->
<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property
name="url"
value="jdbc:mysql://localhost:3306/test"></property>
<property
name="username"
value="root"></property>
<property
name="password"
value="123456"></property>
</bean>
<!-- 声明式事务配置 -->
<!-- 配置事务管理器 -->
<bean
id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property
name="dataSource"
ref="dataSource"
/>
</bean>
<!-- 配置事务通知 -->
<tx:advice
id="txAdvice"
transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
<tx:method
name="add"
propagation="REQUIRED"
/>
<tx:method
name="insert"
propagation="REQUIRED"
/>
<tx:method
name="update"
propagation="REQUIRED"
/>
<tx:method
name="delete"
propagation="REQUIRED"
/>
<tx:method
name="remove*"
propagation="REQUIRED"
/>
<tx:method
name="get"
read-only="true"
/>
<tx:method
name="*"
propagation="REQUIRED"
/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 切入点应该是service.impl下的所有类的所有方法,这里为了方便测试所以直接用了dao.impl -->
<aop:pointcut
expression="execution(* com.liujie.dao.impl.*.*(..))"
id="pointcut"
/>
<aop:advisor
advice-ref="txAdvice"
pointcut-ref="pointcut"
/>
</aop:config>
<!-- 声明式事务配置结束 -->
<!-- 配置sqlSessionFactory -->
<bean
id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property
name="dataSource"
ref="dataSource"></property>
<property
name="configLocation"
value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 自动扫描所配置包下的所有注解
dao:@Repository("userDao")
service:@Service("userService")
action:@Controller("userAction") @Scope("prototype")
属性的注入:@Autowired
-->
<context:component-scan
base-package="com.liujie"></context:component-scan>
</beans>
二、spring注解:
UserDaoImpl.java:
|
@Repository("userDao")
public
class
UserDaoImpl extends
SqlSessionDaoSupport
implements UserDao {
@Autowired
@Override
public
void
setSqlSessionFactory(SqlSessionFactory
sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public
List<User> getAll() {
return
this.getSqlSession().selectList("com.liujie.model.UserMapper.getAll");
}
}
|
|
<bean
id="userDao"
class="com.liujie.dao.impl.UserDaoImpl">
<property
name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
</bean>
|
|
@Repository("userDao"),相当于是创建一个id="userDao"的bean;@Autowired,可以写在要注入的属性上面,也可以写在要注入
的属性的setter方法上面,注入的值是id=属性名的bean。
|
UserServiceImpl.java:
|
@Service("userService")
public
class
UserServiceImpl implements
UserService {
@Autowired
private
UserDao
userDao;
public
void
setUserDao(UserDao userDao) {
this.userDao
=
userDao;
}
public
List<User> getAll() {
return
userDao.getAll();
}
}
|
|
为什么上面UserDaoImpl.java里面,@Repository("userDao")中一定要写"userDao"呢?
因为这里要注入UserDao的属性名为userDao,如果UserDaoImpl.java里面直接写@Repository,默认创建的bean的
id=类名(userDaoImpl),所以要么在UserDaoImpl.java里面写@Repository("userDao"),要么把这里privateUserDaouserDao改
写为privateUserDaouserDaoImpl。
|
UserAction.java:
@Controller("userAction")
@Scope("prototype")
public
class
UserAction {
private
List<User>
list;
@Autowired
private
UserService
userService;
public
void
setUserService(UserService
userService) {
this.userService
=
userService;
}
public
List<User> getList() {
return
list;
}
public
void
setList(List<User> list) {
this.list
=
list;
}
public
String list() {
list
=
userService.getAll();
return
"success";
}
}
三、总结:
|
<bean
id="userDao"
class="com.liujie.dao.impl.UserDaoImpl">
<property
name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
</bean>
<bean
id="userService"
class="com.liujie.service.impl.UserServiceImpl">
<property
name="userDao"
ref="userDao"></property>
</bean>
<bean
id="userAction"
class="com.liujie.action.UserAction"
scope="prototype">
<property
name="userService"
ref="userService"></property>
</bean>
|
| 其实就是用注解的方式来生成这3个bean。 |
本文详细介绍了Spring框架中的配置文件设置及注解使用方法,包括数据源、事务管理、MyBatis集成等内容,并通过具体代码示例展示了如何实现依赖注入。
27万+

被折叠的 条评论
为什么被折叠?



