组件,控制器,业务、仓库
控制器,业务、仓库都是组件的别名
@Component
@Controller
@Service
@Repository
Spring6之Ioc注解的使用
pomxml加入aop的依赖
<!-- 引入Spring context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
SpringConfig.xml主配置文件
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
Bean类上使用注解
value是默认的
组件扫描
<!-- 组件扫描-->
<context:component-scan base-package="com.spring.bean"></context:component-scan>
<!--组件扫描多个包-->
<!-- <context:component-scan base-package="com.spring.bean,com.spring.web"></context:component-scan>-->
<!-- <context:component-scan base-package="com.spring"></context:component-scan>-->
测试类
@Test
public void AnnotationDemo(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
Object studentBean = applicationContext.getBean("StudentBean");
Object userBean = applicationContext.getBean("userBean");
Object orderBean = applicationContext.getBean("OrderBean");
Object teacherBean = applicationContext.getBean("TeacherBean");
System.out.println(studentBean);
System.out.println(userBean);
System.out.println(orderBean);
System.out.println(teacherBean);
}
选择性实例化Bean
<!-- 第一种 use-default-filters="false"如果是false表示这个包下所有带有声明的注解全部失效
<context:component-scan base-package="com.Bean" use-default-filters="false"/>
-->
<context:component-scan base-package="com.Bean" use-default-filters="false">
<!-- 想让那个注解生效就选择那个 只有生效Component -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
<!-- 第二种:
use-default-filters="ture" 所有带有注解的全部生效,不写use-default-filters,则默认为true
-->
<context:component-scan base-package="com.Bean" use-default-filters="true">
<!-- 排除谁不生效 (谁不生效)除了Controller不生效,其他都生效-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
@Value(注入)注解
SpringConfig.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">
<!-- 组件扫描-->
<context:component-scan base-package="com.spring.bean"></context:component-scan>
</beans>
//当属性是简单数据类型的时候,可以使用@Value注解进行注入
@Value是用来代替<property name="name" value="张三"/>
public class Student {
@Value("张三")
private String name;
@Value("20")
private int age;
--get、set、toString--
}
@Autowired(注入)注解
@Autowired可以用来注入非简单类型。单独使用@Autowired注解:默认根据类型装配。[默认byType]
@Autowired可以出现在构造方法,方法,参数,属性,别的注解上。
public interface StudentDao {
void insert();
}
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
@Override
public void insert() {
System.out.println("StudentDaoImpl");
}
}
@Service("StudentService")
public class StudentService {
@Autowired
StudentDao studentDao;
public void test(){
studentDao.insert();
}
}
//测试类
@Test
public void AnnotationInto(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
studentService.test();
}
@Qualifier(注入)注解
//当有多个实现类对象时,@Autowired和Qualifier联合使用,可以根据名称进行装配
//Qualifier可以指定要执行的实现类对象
public interface StudentDao {
void insert();
}
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
@Override
public void insert() {
System.out.println("StudentDaoImpl");
}
}
@Repository("StudentDaoImpl2")
public class StudentDaoImpl2 implements StudentDao{
@Override
public void insert() {
System.out.println("StudentDaoImpl2");
}
}
//无法自动匹配。“studentdao”类型的bean不止一个。由此可见@Autowired不能在多个相同类型的Bean对象中使用。
@Service("StudentService")
public class StudentService {
@Autowired
@Qualifier("StudentDaoImpl2")
StudentDao studentDao;
public void test(){
studentDao.insert();
}
}
//测试
@Test
public void AnnotationInto(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
studentService.test();
}
@Resource注入注解(推荐)
@Resource注解是JDK扩展包中的,属于JDK的一部分。该注解是标准注解,更加具有通用性。(JSR-250标准中指定的注解类型。JSR是Java规范提案)
@Autowired注解是Spring框架的。
@Resource注解默认根据名称装配byName,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。
@Autowired注解默认根据类型装配byType,@Autowired想根据名称装配,需要配合@Qualifier注解一起用。
@Resource注解可以用在属性上,set方法上。
@Autowired注解可以用在属性上,set方法上,构造方法上,构造方法参数上。
@Resource注解是JDK扩展包,不在JDK当中,需要引入依赖。
Spring6不在支持JavaEE,它支持的是JakartaEE9。
Spring6使用这个依赖
Spring6使用这个依赖
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.0</version>
</dependency>
Spring5使用这个依赖
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
public interface StudentDao {
void insert();
}
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
@Override
public void insert() {
System.out.println("StudentDaoImpl");
}
}
@Repository("StudentDaoImpl2")
public class StudentDaoImpl2 implements StudentDao{
@Override
public void insert() {
System.out.println("StudentDaoImpl2");
}
}
@Service("StudentService")
public class StudentService {
//@Autowired
//@Qualifier("StudentDaoImpl2")
@Resource(name ="StudentDaoImpl2")
StudentDao studentDao;
public void test(){
studentDao.insert();
}
}
//测试
@Test
public void AnnotationInto(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
studentService.test();
}
Java类代替SpringConfig.xml配置文件(全注解开发)
//创建一个类来代替SpringConfig.xml
@Configuration
//组件扫描
@ComponentScan({"com.spring.bean","com.spring.Dao"})
public class SpringConfig {
}
//测试
@Test
public void AnnotationInto(){
ApplicationContext applicationContext =new AnnotationConfigApplicationContext(SpringConfig.class);
StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
studentService.test();
}