1.Spring 针对 Bean 管理中创建对象提供注解
- @Component(通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解)
- @Service (标注Service层的服务)
- @Controller(标注web请求控制器)
- @Repository (标注DAO层的数据访问)
上面四个注解功能是一样的,都可以用来创建 bean 实例,但是尽可能具体表明注解
1.1引入依赖
1.2配置bean.xml文件
导入 xmlns:context,更改 xsi:schemaLocation,以及开启组件扫描,可设置过滤器,include以及exclude
<?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="com.atguigu.dao,com.atguigu.service"></context:component-scan>-->
<context:component-scan base-package="com.atguigu"></context:component-scan>
<!-- 只扫描com.atguigu下所有包的Controller注解-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 扫描com.atguigu下的除了Controller的注解-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
1.3 新建UserService类
package com.atguigu.service;
import com.atguigu.dao.UserDao;
import org.springframework.stereotype.Service;
/**
* @author Zhang
* @Description
* @create 2021-04-06 18:23
*/
//在注解里面value属性值可以省略不写
//默认值是UserService ---> userService
//@Service 与下面等同
//@Service(value = "userService")
//也可自定义指定@Service(value = "haha")
@Service(value = "haha")
public class UserService {
private UserDao userDao;
public void add(){
System.out.println("service add......");
userDao.add();
}
}
1.4测试:测试是否能够获取到对象
public void test01(){
//读取xml配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//获取对象
UserService userService = context.getBean("haha", UserService.class);
System.out.println(userService);
userService.add();
}
测试结果
2.基于注解方式实现属性注入
- @Autowired(根据属性类型进行自动装配)
- @Qualifier(根据属性名称进行自动装配,注意,需要结合@Autowired进行使用)
- @Resource(可以根据类型注入,可以根据名称注入)
- @Value(对普通属性进行注入)
3.完全注解开发,配置SpringConfig类
package com.atguigu.configure;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author ZhangZhengtao
* @Description
* @create 2021-04-06 21:33
*/
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
//作用同bean.xml中<context:component-scan base-package="com.atguigu"></context:component-scan>标签
public class SpringConfig {
}
测试
@Test
public void test02(){
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}