IOC操作Bean管理(基于注解关系)
1.什么是注解?
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值..)
(2)使用注解,注解作用在类、方法、属性上。
(3) 使用目的:简化xml配置
2.Bean管理(创建对象)
Spring针对Bean管理中创建对象提供注解
(1)@Component
(2)@Service 一般用于业务逻辑层/service层
(3)@Controller 一般用于web层
(4)@Repository 一般用于do层/持久层
*上面四个注解的功能都是一样的,都可以用来创建bean实例
3.基于注解方式实现对象的创建
第一步 引入依赖
第二步 开启组件扫描
(1)加载头文件
(2)开启扫描
<?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"
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">
<!--开启组件扫描
1 如果扫描多个包 多个包路径 用,隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.wz.spring5.collectiontype"></context:component-scan>
</beans>
第三步 创建类,在类上面添加创建对象注解
//在注解里value属性值可以省略,默认值是类名称的首字母小写(userService)
@Component(value = "userService") //<bean id="" class="">
public class UserService {
public void add(){
System.out.println("service add....");
}
}
4.开启组件扫描的的细节配置
1.设置扫描哪些类、不扫描哪些类
<!--实例1
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter 设置扫描哪些内容 实例中扫描只带Controll注解的类
-->
<context:component-scan base-package="com.wz.spring5" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2.设置不扫描哪些类
<!--实例2
下面配置扫描所有内容
context:exclude-filter 设置哪些内容不进行扫描 下面为不扫描Controller
-->
<context:component-scan base-package="com.wz.spring5">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
5.基于注解方式实现属性注入
(1)@AutoWired:根据属性类型进行自动装配 有多个实现类型时不可以
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
第二步 在service里面注入dao对象,在service类添加dao类型属性,在属性上面使用注解
(2)@Qualifier: 根据属性名称进行注入
这个注解的使用,和@AutoWired一起使用
@Service //<bean id="" class="">
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired
@Qualifier(value = "userDaoImpl1") //根据名称注入
private UserDao userDao;
public void add(){
System.out.println("service add....");
userDao.add();
}
}
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao.add.....");
}
}
(3)@Resource: 可以根据类型注入,可以根据名称注入
name不写为类型注入 写就是名称注入
//@Resource //根据类型注入
@Resource(name = "userDaoImpl1") //根据名称注入
private UserDao userDao;
public void add(){
System.out.println("service add....");
userDao.add();
}
}
(4)@Value:注入普通类型属性
@Value(value = "abc")
private String name;
6.完全注解开发
(1)创建配置类,替代xml配置文件
@Configuration //作为配置类,替代配置文件
@ComponentScan(basePackages = {"com.wz"}) //等价于<context:component-scan base-package="com.wz.spring5"></context:component-scan>
public class SpringConfig {
}
(2)编写测试类
testService1为xml配置 2为完全注解配置
@Test
public void testService1(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
@Test
public void testService2(){
//加载配置类
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}