Spring5
Spring
开源的、免费的、轻量级的、非入侵式的框架(容器),可以实现控制反转IOC、面向切面编程AOP、事务处理
一、Spring的使用
pom.xml文件内
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--解决资源无法生效或无法导出-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
二、IOC思想推导
【重中之重】
1.传统业务
特点:用户的需求可能会迫使我们不停的修改service层或其它层代码
编写Dao层接口
public interface UserDao {
void getUser();
}
编写Dao层实现类
public class UserDaoImpl implements UserDao{
public void getUser() {
System.out.println("默认获取用户数据");
}
}
编写Service业务接口
public interface UserService {
void getUser();
}
编写Service业务实现类
public class UserServiceImpl implements UserService{
private UserDao userDao = new UserDaoImpl();
public void getUser() {
userDao.getUser();
}
}
测试
public class UserTest {
@Test
public void getUser(){
//用户实际调用的是Service层内的业务Service,不需要接触Dao层
UserService userService = new UserServiceImpl();
userService.getUser();
}
}
当有新需求时,我们需要修改代码
编写新UserDaoImpl类
public class UserDaoMysqlImpl implements UserDao{
public void getUser() {
System.out.println("Mysql获取用户数据");
}
}
public class UserDaoOracleImpl implements UserDao{
public void getUser() {
System.out.println("Oracle获取用户数据");
}
}
★★修改UserService的代码★★
public class UserServiceImpl implements UserService{
//用哪个就要new哪个
//private UserDao userDao = new UserDaoImpl();
//private UserDao userDao = new UserDaoMysqlImpl();
private UserDao userDao = new UserDaoOracleImpl();
public void getUser() {
userDao.getUser();
}
}
进行测试
public class UserTest {
@Test
public void getUser(){
//用户实际调用的是Service层内的业务Service,不需要接触Dao层
UserService userService = new UserServiceImpl();
userService.getUser();
}
}
2.控制反转IOC
控制反转指的是:程序由原本的主动创建对象(参考上面★处),变成被动的接受对象(参考下面★处)
利用接口的思想,在service层利用set方法实现动态类型注入
,使用set注入后,程序不再有主动性,从本质上解决了问题,释放了程序员对象的创建极大的降低了耦合性
在serviceImpl类中
public class UserServiceImpl implements UserService{
private UserDao userDao;
//重中之重
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
测试
public class UserTest {
@Test
public void getUser(){
//用户实际调用的是Service层内的业务Service,不需要接触Dao层
UserService userService = new UserServiceImpl();
//★★想用哪个就传哪个★★
((UserServiceImpl)userService).setUserDao(new UserDaoImpl());
userService.getUser();
((UserServiceImpl)userService).setUserDao(new UserDaoMysqlImpl());
userService.getUser();
((UserServiceImpl)userService).setUserDao(new UserDaoOracleImpl());
userService.getUser();
}
}
3.★★★IOC的本质★★★
控制反转IOC(Inversion of Control),是一种设计思想
,DI(依赖注入)是实现IOC的一种方法。
.没有IOC的程序中,我们使用面向对象编程,对象的创建
与对象间的依赖关系
完全硬编码在程序中,对象的创建由程序自己控制
;使用IOC后将对象的创建转移给第三方
,实际就是获得依赖对象的方式反转了
。
对象由Spring来创建、管理、装配
三、HelloSpring
1.简单使用
pojo包下新建实体类
public class Hello {
private String str;
public Hello() {}
public Hello(String str) {this.str = str;}
public String getStr() {return str;}
public void setStr(String str) {this.str = str;}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
resource包下新建applicationContext.xml
解决新建的beans.xml出现Application context not configured for this file
File–>Project Structure–>Modules–>Spring–>选中文件添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring创建对象。在Spring中,这些对象都成为Bean-->
<bean id="hello" class="com.veterlemon.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
测试
public class Mytest {
@Test
public void test(){
//获取Spring的上下文对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过Sping管理取出对象(对象都在Spring管理中)
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
2.日常使用
基于二、IOC思想推导
的代码文件上
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用spring来创建对象-->
<!--id绑定变量名,class绑定对象的全路径,porperty绑定对象的属性指-->
<bean id="hello" class="com.veterlemon.pojo.Hello">
<!--value表示为该简单类型属性赋值-->
<property name="str" value="Spring"/>
</bean>
<bean id="userDaoImpl" class="com.veterlemon.dao.UserDaoImpl"/>
<bean id="userDaoMysqlImpl" class="com.veterlemon.dao.UserDaoMysqlImpl"/>
<bean id="userDaoOracleImpl" class="com.veterlemon.dao.UserDaoOracleImpl"/>
<bean id="userServiceImpl" class="com.veterlemon.service.UserServiceImpl">
<!--ref表示引用Spring中创建的对象,即上面的几个bean的id-->
<property name="userDao" ref="userDaoMysqlImpl"/>
</bean>
</beans>
测试
public class Mytest {
@Test
public void test(){
//获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过Sping管理取出对象(对象都在Spring管理中)
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
//调用方法
userServiceImpl.getUser();
}
}
修改需求,要求访问Oraclce,只需修改applicationContext.xml的ref引用即可
<property name="userDao" ref="userDaoOracleImpl"/>
3.IOC创建对象的方式
当实体类中只有有参构造时,需要修改其bean的写法
public class User {
private String name;
public User(String name) {this.name= name;}
public String getName() {return name;}
public void setName(String name) {this.name= name;}
}
方式一:通过index赋值
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg index="0" value="张三"/>
</bean>
方式二:通过type赋值,不建议使用
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg type="java.lang.String" value="张三"/>
</bean>
方式三:直接通过参数名设置,建议使用
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg name="name" value="张三"/>
</bean>
总代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--既有有参又有无参时,用IOC创建对象-->
<!--<bean id="user" class="com.veterlemon.pojo.User">-->
<!-- <property name="name" value="张三"/>-->
<!--</bean>-->
<!--实体类只有有参构造时用IOC创建对象,方式一:通过index赋值-->
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg index="0" value="张三"/>
</bean>
<!--实体类只有有参构造时用IOC创建对象,方式二:通过type赋值,不建议使用-->
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg type="java.lang.String" value="张三"/>
</bean>
<!--实体类只有有参构造时用IOC创建对象,方式三:直接通过参数名设置,建议使用-->
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg name="name" value="张三"/>
</bean>
</beans>
4.关于报错
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [Spring.xml] cannot be opened because it does not exist
可能1:没有解决文件映射
见上
可能2:没有在pom.xml文件的build中添加
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
可能3:测试类处文件地址与其真实地址不符合
四、Spring的配置
1.别名
在文件中设置别名
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--为user起别名-->
<alias name="user" alias="suibianqi"/>
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg name="name" value="张三"/>
</bean>
</beans>
测试
public class Mytest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过Sping管理取出对象
User user = (User) context.getBean("user");
//通过别名取出对象
User suibianqi = (User) context.getBean("suibianqi");
user.show();
suibianqi.show();
}
}
2.Bean的配置
<!--
id:bean的唯一标识符,相当于变量名
class:bean对象所对应的全限定名(全限定名:包名 + 对象类名)
name:也是别名,比 alias标签 更高级(可以同时取多个)
-->
<bean id="user" class="com.veterlemon.pojo.User">
<constructor-arg name="name" value="张三"/>
</bean>
3.import
一般用于团队开发,可以将多个配置文件导入并合为一个,方便使用
有多个 beans.xml 时,可以在 applicationContext.xml 文件中通过import标签合成一个,再调用 applicationContext.xml 即可完成对多个 beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--导入其它beans.xml文件-->
<import resource="beans1.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
</beans>
五、依赖注入
1.构造器注入
如上述
2.Set方式注入【重点】
依赖注入本质:set方式注入
(依赖:bean对象的创建依赖于容器;注入:bean对象的所有属性由容器注入)
【环境搭建】
在pojo包下新建实体类
//地址实体类
public class Address {
private String address;
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
//学生实体类
public class Student {
private String name;
//引用类型
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String , String > card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public String[] getBooks() { return books; }
public void setBooks(String[] books) { this.books = books; }
public List<String> getHobbys() { return hobbys; }
public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; }
public Map<String, String> getCard() { return card; }
public void setCard(Map<String, String> card) { this.card = card; }
public Set<String> getGames() { return games; }
public void setGames(Set<String> games) { this.games = games; }
public String getWife() { return wife; }
public void setWife(String wife) { this.wife = wife; }
public Properties getInfo() { return info; }
public void setInfo(Properties info) { this.info = info; }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.veterlemon.pojo.Address">
<property name="address" value="来福客栈"/>
</bean>
<bean id="student" class="com.veterlemon.pojo.Student">
<!--1.普通注入,通过value赋值-->
<property name="name" value="张三"/>
<!--2.Bean注入,ref引入复杂类型-->
<property name="address" ref="address"/>
<!--3.数组注入-->
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
<!--4.List集合注入-->
<property name="hobbys">
<list>
<value>写代码</value>
<value>睡觉</value>
<value>打游戏</value>
</list>
</property>
<!--Map集合注入-->
<property name="card">
<map>
<entry key="性别" value="男"/>
<entry key="手机号码" value="111111111"/>
<entry key="邮箱" value="123@123.com"/>
</map>
</property>
<!--Set集合注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
<value>APEX</value>
</set>
</property>
<!--null值注入-->
<property name="wife">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="stu_id">123123</prop>
<prop key="math_score">90</prop>
<prop key="english_score">60</prop>
</props>
</property>
</bean>
</beans>
测试
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());;
/*
结果:
Student{
name='张三',
address=Address{address='来福客栈'},
books=[西游记, 红楼梦, 水浒传],
hobbys=[写代码, 睡觉, 打游戏],
card={性别=男, 手机号码=111111111, 邮箱=123@123.com},
games=[LOL, CF, APEX], wife='null',
info={stu_id=123123, english_score=60, math_score=90}
}
*/
}
}
3.拓展方式注入
p命名空间注入(对应set注入方式,使用p(property)注入
)
要添加p命名空间注入的声明:xmlns:p=“http://www.springframework.org/schema/p”
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性-->
<bean id="user" class="com.veterlemon.pojo.User" p:name="李四" p:age="38"/>
</beans>
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);;
}
c命名空间注入(对应构造器注入,实体类中没有构造器就无法使用,c(construct-args)
)
要添加c命名空间注入的声明:xmlns:c=“http://www.springframework.org/schema/c”
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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--c命名空间注入,可以直接注入属性,只能在构造器的情况下使用-->
<bean id="user2" class="com.veterlemon.pojo.User" c:name="王五" c:age="28"/>
</beans>
测试
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user2 = context.getBean("user2", User.class);
System.out.println(user2);;
}
4.bean的作用域
单例模式(Spring的默认机制)
<bean id="user2" class="com.veterlemon.pojo.User" c:name="王五" c:age="28" scope="singleton"/>
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user==user2); //结果为true,说明无论创建多少个对象,其实都是同一个
}
原型模式
每次从容器中get时,都会产生新对象
<bean id="user2" class="com.veterlemon.pojo.User" c:name="王五" c:age="28" scope="prototype"/>
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
User user = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user==user2); //结果为false,说明不是同一个对象
}
其余的request、session、application模式都只能在web开发中使用
六、Bean的自动装配
自动装配是Spring满足bean依赖的一种方式Spring会在上下文中自动寻找并自动给bean装配属性
1.装配方式一
在xml文件中显式(手动)配置
2.装配方式二
在java中显式(手动)配置
3.装配方式三【重点】
(1) 隐式自动装配bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dag" class="com.veterlemon.pojo.Dog"></bean>
<bean id="cat" class="com.veterlemon.pojo.Cat"></bean>
<!--自动配置形式一:byName-->
<!--byName原理:会自动在容器上下文中查找(查找自己实体类中与【set方法名】对应的bean id,需要保证id唯一)-->
<!--<bean id="person" class="com.veterlemon.pojo.Person" autowire="byName">-->
<!--自动配置形式二:byType-->
<--byType原理:会自动在容器上下文中查找(查找自己实体类中与【属性类型】相同的bean class,可以省略id的设置)-->
<bean id="person" class="com.veterlemon.pojo.Person" autowire="byType">
<property name="name" value="张三"/>
<!--手动配置方式-->
<!--<property name="dog" ref="dag"/>-->
<!--<property name="cat" ref="cat"/>-->
</bean>
</beans>
(2) 使用注解进行自动装配
pojo包下的实体类
①@Autowired:为复杂类型导入自动装配注解,默认使用byType方式,当匹配到多个同类型时使用byName,@Autowired(required=false):可让该对象为null;@Nullable注解,可让该字段为null
②@Qualifier(value = "dag"):当AutoWired的装配环境比较复杂(有多个类似的bead id),用Qualifier申明bean的指向,通常与AutoWired配套使用
③@Resource(value = "cat")等价于@Autowired+@Qualifier(value = "cat"),需要导jar包,默认使用byName方式,当匹配到多个同bean id时使用byType
//Person实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Person {
//导入注解
@Autowired(required=false)
@Qualifier(value = "dog")
private Dog dog;
//导入注解
@Autowired
@Qualifier(value = "cat")
private Cat cat;
private String name;
}
//Dog实体类
public class Dog {
public void shout(){
System.out.println("wangwangwang");
}
}
//Cat实体类
public class Cat {
public void shout(){
System.out.println("miaomiaomiao");
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注解支持-->
<context:annotation-config/>
<bean id="dag" class="com.veterlemon.pojo.Dog"></bean>
<bean id="dag22" class="com.veterlemon.pojo.Dog"></bean>
<bean id="cat" class="com.veterlemon.pojo.Cat"></bean>
<bean id="cat22" class="com.veterlemon.pojo.Cat"></bean>
<bean id="person" class="com.veterlemon.pojo.Person" autowire="byType"/>
</beans>
测试
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person.getName());
person.getDog().shout();
person.getCat().shout();
}
七、使用注解进行开发
1.环境搭建
导入依赖
https://editor.csdn.net/md/?articleId=123555841
添加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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
添加实体类
public class User {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
2.实现自动装配
配置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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定要扫描的包,使该包下的注解生效-->
<context:component-scan base-package="com.veterlemon"/>
<!--<context:annotation-config/>-->
</beans>
修改实体类,实现自动装配:@Component,注入属性值:@Value(“value”)
使用@Component 即是在配合<context:component-scan>标签
@Component 等价于<bean id="user" class="com.veterlemon.pojo.User"/>
@Value("张三") 等价于<property name="name" value="张三"/>
@Component
public class User {
private String name;
public String getName() { return name; }
@Value("张三")
public void setName(String name) { this.name = name; }
}
3.@Component的衍生注解
@Component的衍生注解,配合MVC架构 (四个注解功能一致)
pojo使用:@Component
Dao使用:@Repository
Service实现类使用:@Service
Controller使用:@Controller
4.作用域
在实体类中使用@Scope
@Component
@Scope("singleton | prototype | ...")
public class User {
private String name;
public String getName() { return name; }
@Value("张三")
public void setName(String name) { this.name = name; }
}
5.注解与xml方式的区别
xml方式:更加万能,适用于任何场合,维护简单方便使用xml适合:管理bean
注解方式:仅限于使用自己类,维护相对复杂注解适合:负责完成属性的注入
八、使用Java的方式配置Spring
不使用Sping的xml配置,全权由Java实现 (在SpringBoot中常用)
1.环境搭建
新建实体类
@Component
public class User {
private String name;
public String getName() { return name; }
@Value("张三")
public void setName(String name) { this.name = name; }
}
新建config包,在包下新建Config配置类
@Bean相当于bean标签,方法名=bean标签中的id属性,返回值=bean标签中的class值
@Configuration //表示是一个配置类
@ComponentScan("com.veterlemon.pojo") //表示扫描包,使注解生效
@Import(Config2.class) //表示引入其它Config
public class Config {
//注册bean,@Bean相当于bean标签
@Bean
//绑定方法,方法名相=bean标签中的id属性,返回值=bean标签中的class值
public User getUser(){ return new User(); }
}
测试
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.getName());
}
九、代理模式
代理模式是Spring AOP的底层
1.静态代理
★★在不改变原有业务代码的情况下,扩展功能
★★
抽象角色:一般使用接口或抽象类来实现
真实角色:被代理的人
代理角色:代理真实角色
客户:访问代理角色的人
真实对象和代理对象要实现同一个接口,代理对象代理真实对象
好处:使真实角色的操作更加纯粹,公共业务交给代理角色,实现了业务的分工和业务的扩展
坏处:一个真实角色就要对应一个代理角色,导致代码量增多、开发效率变低
//抽象角色:出租房子
public interface Rent {
public void rent();
}
//真实角色:房东
public class Host implements Rent{
@Override
public void rent() {
System.out.println("房东要出租房子");
}
}
//代理角色:中介
public class Proxy implements Rent{
private Host host;
public Proxy() { }
public Proxy(Host host) { this.host = host; }
@Override
public void rent() { host.rent(); }
//代理特有方法:看房
public void seeHouse(){
System.out.println("中介带你租房子");
}
//代理特有方法:签合同
public void getContract(){
System.out.println("中介带你签合同");
}
//代理特有方法:收中介费
public void getCharge(){
System.out.println("中介收中介费");
}
}
//客户:要租房的人
public class Client {
public static void main(String[] args) {
//房东要出租房子
Host host = new Host();
//通过中介租房子
Proxy proxy = new Proxy(host);
//客户租房子
proxy.rent();
}
}
★★在不改变原有业务代码的情况下,扩展功能
★★
//抽象对象
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
//真实角色
public class UserServiceImpl implements UserService{
@Override
public void add() { System.out.println("add someone"); }
@Override
public void delete() { System.out.println("delete someone"); }
@Override
public void update() { System.out.println("update someone"); }
@Override
public void query() { System.out.println("query someone"); }
}
//代理角色:往UserService的每个方法里都加一句输出
public class UserServiceProxy implements UserService{
private UserServiceImpl userService;
@Override
public void add() {
log("add");
userService.add();
}
@Override
public void delete() {
log("delete");
userService.delete();
}
@Override
public void update() {
log("update");
userService.update();
}
@Override
public void query() {
log("query");
userService.query();
}
//添加公共方法
public void log(String msg){ System.out.println("使用了"+msg+"方法"); }
public UserServiceProxy() { }
public UserServiceProxy(UserServiceImpl userService) { this.userService = userService; }
public UserServiceImpl getUserService() { return userService; }
public void setUserService(UserServiceImpl userService) { this.userService = userService; }
}
//客户
public class Client {
public static void main(String[] args) {
//实例化真实角色
UserServiceImpl userService = new UserServiceImpl();
//实例化代理角色
UserServiceProxy proxy = new UserServiceProxy();
//通过代理角色实现输出
proxy.setUserService(userService);
proxy.add();
}
}
2.动态代理
动态代理的本质:基于JDK的反射机制
解决静态代理一个真实对象就要对应一个代理对象,代码量提高
的问题,动态体现在代理对象是在程序运行时动态生成的
(1) 基于接口的动态代理 (JDK动态代理)一个动态代理类代理接口,一般对应一类的业务
(2) 基于类的动态代理 (cglib动态代理,使用第三方工具库,该类必须能够继承)
// 1.创建真实角色类(目标类),并添加方法
public class UserServiceImpl implements UserService{
@Override
public void add() { System.out.println("add someone"); }
@Override
public void delete() { System.out.println("delete someone"); }
@Override
public void update() { System.out.println("update someone"); }
@Override
public void query() { System.out.println("query someone"); }
}
// 2.创建InvocationHandler的实现类,在该类中给目标方法增加功能(InvocationHandler:调用处理程序)
public class ProxyInvocationHandler implements InvocationHandler {
//被代理的对象(目标对象)
private Object target;
//创建代理对象的方法
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
//通过动态代理对象对方法进行增强
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//代理对象执行方法时会调用invoke()
log(method.getName());
//执行目标类的方法(通过Method类实现)
Object result = method.invoke(target, args);
//返回目标方法执行结果
return result;
}
//自定义方法
public void log(String msg){ System.out.println("执行了" + msg + "方法"); }
public Object getTarget() { return target; }
public void setTarget(Object target) { this.target = target; }
}
// 创建客户类
public class Client {
public static void main(String[] args) {
//创建目标对象
UserServiceImpl userService = new UserServiceImpl();
// 3.创建代理角色(现在不存在)
ProxyInvocationHandler pih = new ProxyInvocationHandler();
// 3.1 设置代理对象
pih.setTarget(userService);
// 3.2 动态生成代理类
UserService proxy = (UserService) pih.getProxy();
// 3,3代理对象执行方法 (会调用InvocationHandler的invoke()方法 )
proxy.add();
}
}
十、★★★★AOP★★★★
AOP:面向切面编程,通过预编译方式和动态代理实现程序功能的统一维护
AOP底层就是采用动态代理模式实现的,它把实现动态代理的步骤进行了一个规范
优点:提供声明式事务,允许用户自定义切面,在不改变原业务代码的基础上实现添加额外业务功能 (提扩展、解耦合、减冗余)
1.Spring-AOP的依赖
导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
2.使用Spring实现AOP
方式一:使用Spring的接口
// 1.定义抽象对象类
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
// 2.定义前置执行方法类
public class BeforeLog implements MethodBeforeAdvice {
@Override
//method:要执行的【目的对象】的【方法】,args:参数,target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
// 3.定义后置执行方法类
public class AfterLog implements AfterReturningAdvice {
@Override
//returnValue:返回值,method:要执行的【目的对象】的【方法】,args:参数,target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
// 4.编写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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定要扫描的包,使该包下的注解生效-->
<context:component-scan base-package="com.veterlemon"/>
<context:annotation-config/>
<!--注册bean-->
<bean id="userSerivce" class="com.veterlemon.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.veterlemon.log.BeforeLog"/>
<bean id="afterLog" class="com.veterlemon.log.AfterLog"/>
<!--导入aop约束-->
<!--方式一:使用原生Spring API接口-->
<!--配置aop-->
<aop:config>
<!--切入点:需要在哪个地方执行spring方法-->
<!--expression="execution(要执行的位置)"-->
<aop:pointcut id="pointcut" expression="execution(* com.veterlemon.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
// 5.测试
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口,故使用 UserService
UserService userSerivce = context.getBean("userSerivce", UserService.class);
userSerivce.add();
}
}
方式二:使用自定义类实现
// 1.定义抽象对象类
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
// 2.自定义切入点类
public class DiyPointcut {
public void beforeCut(){
System.out.println("方法执行前");
}
public void afterCut(){
System.out.println("方法执行后");
}
}
// 3.编写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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定要扫描的包,使该包下的注解生效-->
<context:component-scan base-package="com.veterlemon"/>
<context:annotation-config/>
<!--注册bean-->
<bean id="userSerivce" class="com.veterlemon.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.veterlemon.log.BeforeLog"/>
<bean id="afterLog" class="com.veterlemon.log.AfterLog"/>
<!--方式二-->
<bean id="diyPointcut" class="com.veterlemon.diy.DiyPointcut"/>
<aop:config>
<!--自定义切面,ref指向自定义切入点类-->
<aop:aspect ref="diyPointcut">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.veterlemon.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="beforeCut" pointcut-ref="pointcut"/>
<aop:before method="afterCut" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
// 4.测试
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口,故使用 UserService
UserService userSerivce = context.getBean("userSerivce", UserService.class);
userSerivce.add();
}
}
方式三:使用注解配置AOP
没有@Aspect的去掉Spring-AOP依赖的scope
// 1.定义切面类
@Aspect
@Component
public class AnnotationPointcut {
// 2.定义通知和切点
@Before("execution(* com.veterlemon.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("注解方式,方法执行前");
}
// 2.定义通知和切点
@After("execution(* com.veterlemon.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("注解方式,方法执行后");
}
// 2.定义通知和切点
//环绕通知。可以传参数,表示要获取处理切入的点
@Around("execution(* com.veterlemon.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
//执行方法
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
}
// 3.配置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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定要扫描的包,使该包下的注解生效-->
<context:component-scan base-package="com.veterlemon"/>
<context:annotation-config/>
<!--注册bean-->
<bean id="userSerivce" class="com.veterlemon.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.veterlemon.log.BeforeLog"/>
<bean id="afterLog" class="com.veterlemon.log.AfterLog"/>
<!--导入aop约束-->
<!--方式三-->
<bean id="annotationPointcut" class="com.veterlemon.diy.AnnotationPointcut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
//测试
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userSerivce = context.getBean("userSerivce", UserService.class);
userSerivce.add();
}
3.★使用Spring的aspectj框架实现AOP★
(1) 切点的执行时间 (Advice)
在aspectj框架中使用注解表示,也可以使用xml配置文件中的标签
1) @Before
2) @AfterReturning
3) @Around
4) @AfterThrowing
5) @After
(2) 表达切面的执行位置 (切点表达式)
(3) 实现AOP的基本步骤
aspectj依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
// 1.定义目标接口类
public interface SomeService {
void doSome(String name, Integer age);
}
// 2.定义目标接口实现类
public class SomeServiceImpl implements SomeService {
@Override
public void doSome(String name, Integer age) {
//要求:给doSome方法添加一个功能,在doSome()执行前输出
System.out.println("====目标方法doSome()====");
}
}
// 3.定义切面类
@Aspect
public class MyAspect {
/**
* 定义方法,实现业务功能拓展
* 要求:
* 1.公共方法
* 2.方法没有返回值
*/
@Before(value = "execution(* com.veterlemon.service.SomeServiceImpl.doSome(..))")
public void myBefore(){
//切面要执行的功能
System.out.println("前置通知-切面内容,在目标方法执行之前执行,执行时间为:" + new Date());
}
@Before(value = "execution(* com.veterlemon.service.SomeServiceImpl.*(..))")
public void myBefore2(){
//切面要执行的功能
System.out.println("前置通知-切面内容(简化切点表达式,一次执行多个切面方法),在目标方法执行之前执行,执行时间为:" + new Date());
}
}
// 4.配置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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--声明目标对象-->
<bean id="someService" class="com.veterlemon.service.SomeServiceImpl"/>
<!--声明切面类对象-->
<bean id="myAspect" class="com.veterlemon.aspect.MyAspect"/>
<!--声明自动代理生成器,aop:aspectj-autoproxy会把spring容器中的所有【目标对象】一次性都生成【代理对象】-->
<aop:aspectj-autoproxy/>
</beans>
// 5.测试
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取目标对象 (动态代理代理的是接口)
SomeService someService = context.getBean("someService", SomeService.class);
//通过代理的对象执行方法
someService.doSome("张三", 28);
}
十一、Spring整合MyBatis
使用的技术:IOC
1.MyBatis的使用步骤
2.整合
1、pom.xml文件中添加依赖
https://editor.csdn.net/md/?articleId=123555841
2、pojo包下创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class User {
private int id;
private String name;
private String pwd;
}
3、dao包下创建Mapper接口和Mapper.xml文件
//UserMapper接口
public interface UserMapper {
public List<User> getUser();
}
//UserMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.veterlemon.dao.UserMapper">
<select id="getUser" resultType="user">
select * from mybatis.user;
</select>
</mapper>
4、创建mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.veterlemon.pojo"/>
</typeAliases>
<mappers>
<package name="com.veterlemon.dao"/>
</mappers>
</configuration>
5、service包下创建service接口和实现类
//UserService接口类
public interface UserSerivce {
public List<User> getUser();
}
//UserServiceImpl实现类
public class UserServiceImpl implements UserService {
//引用类型
private UserMapper userDao;
@Override
public List<User> getUser() {
List<User> userList = userDao.getUser();
return userList;
}
//使用set注入 (IOC)
public void setUserDao(UserMapper userDao) { this.userDao = userDao; }
}
6、创建Spring配置文件 (此处的配置文件是为了控制Dao层,故起名spring-dao.xml,最后需要在applicationContext.xml中引入spring-dao.xml)
//jdbc.properties文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
//spring-dao.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--DataSource:使用Spring的数据源替代MyBatis的配置,Spring原生连接与Druid连接二选一-->
<!--<bean id="mydataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<bean id="mydataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--声明SqlSessionFactoryBean,这个类创建sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--set注入,把数据库连接交付给dataSource属性-->
<property name="dataSource" ref="mydataSource"/>
<!--绑定MyBatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--<property name="mapperLocations" value="classpath:com/veterlemon/dao/*.xml"/>-->
</bean>
<!--创建dao对象,使用SqlSession的getMapper(xxMapper.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定sqlSessionFactory的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定包名 (dao所在的包),MapperScannerConfigurer会扫描这个包下的所有接口,执行并得到每个接口的dao对象-->
<property name="basePackage" value="com.veterlemon.dao"/>
</bean>
<!--声明service,调用dao层-->
<bean id="userService" class="com.veterlemon.service.UserServiceImpl">
<property name="userDao" ref="userMapper"/>
</bean>
</beans>
7、测试
public class Test {
@org.junit.Test
public void DaoTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.getUser()) {
System.out.println(user);
}
}
@org.junit.Test
public void ServiceTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
for (User user : userService.getUser()) {
System.out.println(user);
}
}
}
3.事务
spring的事务处理模型:抽象了事务处理的各个方面,定义了事务处理的步骤
声明式事务 (AOP的方式)
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/cache/spring-tx.xsd">
<!--引入jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--DataSource:使用Spring的数据源替代MyBatis的配置-->
<!--<bean id="mydataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<bean id="mydataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mydataSource"/>
<!--绑定MyBatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--<property name="mapperLocations" value="classpath:com/veterlemon/dao/*.xml"/>-->
</bean>
<!--创建dao对象,使用SqlSession的getMapper(xxMapper.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定sqlSessionFactory的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定包名 (dao所在的包),MapperScannerConfigurer会扫描这个包下的所有接口,执行并得到每个接口的dao对象-->
<property name="basePackage" value="com.veterlemon.dao"/>
</bean>
<!--声明service,调用dao层-->
<bean id="userService" class="com.veterlemon.service.UserServiceImpl">
<property name="userDao" ref="userMapper"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mydataSource"/>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务的类-->
<tx:advice id="txAdvice" transaction-manager="transactionManage">
<!--给哪些方法配置事务,选择配置事务的传播特性-->
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<!--为所有方法配置事务-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.veterlemon.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
编程式事务 (在代码中进行事务的管理)