IOC操作Bean管理(基于注解创建对象、基于注解组件扫描配置、基于注解方式实现属性注入、完全注解开发)
作者:互联网
文章目录
一、什么是注解?
*1.注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
*2.使用注解,注解可以作用在类上面,方法上面,属性上面
*3.使用注解目的:简化xml配置
二、基于注解创建对象
**常用四个注解:
*1.@Component
*2.@Service
*3.@Controller
*4.@Repository
*上面四个注解功能是一样的,都可以用来创建bean实例。
步骤如下:
*1.引入依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.17</version>
</dependency>
*2. ,引入名称空间,开启组件扫描:(扫描哪个包中哪个类需要注解)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开始组件扫描:
1.要扫描多个包,中间使用逗号隔开
2.扫描上层目录
-->
<context:component-scan base-package="cn.hncj"></context:component-scan>
</beans>
*3.创建类,在类上面添加创建对象
//在注解里value属性值可以不写,默认值是类名称,首字母小写
@Component(value = "userService") //<bean id="userService" class="">
public class UserService {
public void add(){
System.out.println("service add...");
}
}
//测试类
public class Demo01 {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
三、开启组件扫描细节配置:
<!--示例1
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter type="annotation" expression="org.springframework.stereotype.Component"表示扫描带有Component的类
-->
<context:component-scan base-package="cn.hncj" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
<!--示例2
下面配置扫描包所有内容
context:exclude-filter,设置哪些内容不扫描
-->
<context:component-scan base-package="cn.hncj">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
</beans>
四、基于注解方式实现属性注入:
属性注入的注解:
@AutoWired:根据属性类型进行自动装配
第一步:把service和dao对象创建,在service和到类添加创建对象注解
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("dao add...");
}
}
@Service
public class UserService {
public void add(){
System.out.println("service add...");
}
}
第二步:在service中注入dao对象,先在service类添加dao类型属性,再在属性上面使用注解
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired //根据类型进行注入
private UserDao userDao;
public void add() {
System.out.println("service add...");
userDao.add();
}
}
//测试类
public class Demo01 {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
@Qualifier:根据属性名称进行注入,这个注解需要和上面的@AutoWired一起使用
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("dao add...");
}
}
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired
@Qualifier(value = "userDaoImpl1") //根据名称进行注入
private UserDao userDao;
public void add() {
System.out.println("service add...");
userDao.add();
}
public class Demo01 {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
@Resource:可以根据类型注入,可以根据名称注入
*仅修改UserService中注解如下,结果相同。
@Service
public class UserService {
//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1")
private UserDao userDao;
public void add() {
System.out.println("service add...");
userDao.add();
}
}
@Value:注入普通类型属性
@Service
public class UserService {
//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1")
private UserDao userDao;
@Value(value ="abc")
private String name;
public void add() {
System.out.println("service add..."+name);
userDao.add();
}
}
public class Demo01 {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
五、完全注解开发:
*1.创建配置类,替代xml配置文件 : 在cn.hncj包下创建config包,创建SpringConfig类
package cn.hncj.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"cn.hncj"})
public class SpringConfig {
}
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("dao add...");
}
}
@Service
public class UserService {
//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1")
private UserDao userDao;
@Value(value ="abc")
private String name;
public void add() {
System.out.println("service add..."+name);
userDao.add();
}
}
//测试类
public class Demo2 {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
标签:基于,创建对象,public,add,userService,注解,class,UserService 来源: https://blog.csdn.net/weixin_45139640/article/details/123617825