目录
- 改造基于注解的IOC案例,使用纯注解的方式实现
spring的一些新注解的使用
- Spring和Juint的整合
1. 基于注解的IOC配置
1.1 明确
学习基于注解的IOC配置,注解配置和xml配置实现的功能是一样的,都是降低程序间的耦合,只是配置形式不一样。
关于实际的开发中到底使用xml还是注解,每家公司有不同的使用习惯。
1.2 常用的IOC注解按照作用分类
< bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="" init-method="" destory-method=''>
<property name="" value="" / ref=""></property>
</ bean>
1.用于创建对象的
他们的作用就和在xml配置文件中编写一个< bean>标签实现的功能是一样的
2.用于注入数据的
他们的作用就和在xml配置文件中的bean标签中写一个property标签的作用是一样的
3.用于改变作用范围的
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
4.和生命周期相关的
他们的作用就是在bean标签中使用init-method和destory-method的作用是一样的
1.2.1 用于创建对象的注解
1. @Component
作用:用于把当前类对象(通过反射创建)存入spring容器中(spring容器中都是map形式,当前类对象相当于value)
属性:value:用于指定bean的id(相当于key)。当我们不写时,它的默认值是当前类名,且首字母改小写
但是还需要在bean.xml中告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context的名称空间(xmlns:context=“http://www.springframework.org/schema/context”)的约束中。
- bean.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"
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">
<!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context的名称空间的约束中.component-scan标签会扫描包及其子包下所有类和接口的注解-->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
- AccountServiceImpl.java
/**
* 账户的业务层实现类
*/
@Component(value = "accountService") //value即对象的id,默认为类名首字母小写
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = new AccoutDaoImpl();
public AccountServiceImpl(){
System.out.println("AccountService创建了。");
}
public void saveAccount(){
accountDao.saveAccount();
}
}
- 细节:@Component如果只有一个value属性,可以不用写,如@Component(“accountService”);如果同时有两个属性,那么必须写@Component(value = “accountService”)
- Client.java
public class Client {
/**
* 获取spring的IOC核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
//1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService as = (IAccountService) ac.getBean("accountService"); //使用value即id获取对象
System.out.println(as);
//as.saveAccount();
}
}
2. @Controller
一般用在表现层
作用和属性和@Component一模一样,是spring框架为我们提供的明确的三层使用的注解,使我们的三层对象更加清晰
3. @Service
一般用在业务层
作用和属性和@Component一模一样,是spring框架为我们提供的明确的三层使用的注解,使我们的三层对象更加清晰
4. @Repository
一般用在持久层
作用和属性和@Component一模一样,是spring框架为我们提供的明确的三层使用的注解,使我们的三层对象更加清晰
1.2.2 用于注入数据的
1. @Autowired
作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
如果IOC容器中没有任何bean的类型和要注入的类型匹配时,则报错;
如果IOC容器中有多个类型匹配时,首先根据类型查找Map中是否存在相同类型,如果存在相同类型,则根据变量名在相同类型中
出现位置:可以是成员变量,也可以是方法
细节:在使用注解注入时,set方法不是必须的
实现方法:spring的IOC容器是Map结构的,Map<String, Object>其中key是@Component注解中的value,Object是@Component注解注入的类。@Autowired是根据类型注入的,AccountServiceImpl .java中通过查找Map中是否有相同的类型(IAccountDao 类型),如果类型匹配,那么就会注入成功;如果将AccountDaoImpl.java中的implements IAccountDao注释掉,那么找不到匹配类型,注入失败
- bean.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"
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">
<!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context的名称空间的约束中-->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
- AccountServiceImpl.java
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccoutDaoImpl;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 账户的业务层实现类
*/
@Component("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public void saveAccount(){
accountDao.saveAccount();
}
}
以上三个注解都只能注入其他bean类型的数据,而基本类型和string类型无法使用上述注解实现,另外,集合类型注入只能通过xml来实现
4. @Value
作用:用于注入基本类型和String类型的数据
属性:
value:用于指定数据的值,可以使用spring中的SpEL(也就是spring中的El表达式)
SpEL的写法也是${表达式}
1.2.3 用于改变作用范围的
1. @Scope
作用:用于指定bean的作用范围
属性:
value:指定范围的取值,常用取值:singleton(默认)、prototype
- AccountServiceImpl.java
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccoutDaoImpl;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
* 账户的业务层实现类
*/
@Component("accountService")
@Scope("prototype") //多例
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao1")
private IAccountDao accountDao2;
public void saveAccount(){
accountDao2.saveAccount();
}
}
- Client.java
package com.itheima.ui;
import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
* 获取spring的IOC核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
//1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
IAccountService as2 = (IAccountService) ac.getBean("accountService");
// System.out.println(as);
//
// IAccountDao adao = (IAccountDao) ac.getBean("accountDao");
// System.out.println(adao);
// as.saveAccount();
System.out.println(as == as2);
}
}
- 输出
一月 22, 2020 4:04:08 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@77a567e1: startup date [Wed Jan 22 16:04:08 CST 2020]; root of context hierarchy
一月 22, 2020 4:04:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
false
1.2.4 和生命周期相关的
1. @PreDestroy
作用:用于指定销毁方法
2. @PostConstruct
作用:用于指定初始化方法
- AccountServiceImpl.java
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccoutDaoImpl;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
/**
* 账户的业务层实现类
*/
@Component("accountService")
@Scope("singleton") // 修改为单例对象
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao1")
private IAccountDao accountDao2;
@PostConstruct
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destroy(){
System.out.println("销毁了");
}
public void saveAccount(){
accountDao2.saveAccount();
}
}
- Client.java
package com.itheima.ui;
import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
* 获取spring的IOC核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
//1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); // 使用子类
//2、根据id获取bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
// IAccountService as2 = (IAccountService) ac.getBean("accountService");
// System.out.println(as);
//
// IAccountDao adao = (IAccountDao) ac.getBean("accountDao");
// System.out.println(adao);
as.saveAccount();
ac.close(); //关闭
}
}