一、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"),要么把这里
private
UserDao
userDao改
写为
private
UserDao
userDaoImpl。
|
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。 |