首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >手拉手入门Spring6之Ioc注解开发

手拉手入门Spring6之Ioc注解开发

原创
作者头像
QGS
发布2023-03-19 11:37:09
发布2023-03-19 11:37:09
4300
举报
文章被收录于专栏:QGS星球QGS星球
代码语言:javascript
复制
组件,控制器,业务、仓库
控制器,业务、仓库都是组件的别名

@Component
@Controller
@Service
@Repository

Spring6之Ioc注解的使用

代码语言:javascript
复制
pomxml加入aop的依赖

 <!-- 引入Spring context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.0-M2</version>
        </dependency>
代码语言:javascript
复制
SpringConfig.xml主配置文件

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
代码语言:javascript
复制
Bean类上使用注解
value是默认的
代码语言:javascript
复制
组件扫描
<!--        组件扫描-->
        <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>-->
代码语言:javascript
复制
测试类
@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

代码语言:javascript
复制
<!--    第一种 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(注入)注解

代码语言:javascript
复制
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>
代码语言:javascript
复制
//当属性是简单数据类型的时候,可以使用@Value注解进行注入
@Value是用来代替<property name="name" value="张三"/>
代码语言:javascript
复制
public class Student {
    @Value("张三")
    private String name;
    @Value("20")
    private int age;
--get、set、toString--
}

@Autowired(注入)注解

代码语言:javascript
复制
@Autowired可以用来注入非简单类型。单独使用@Autowired注解:默认根据类型装配。[默认byType]
@Autowired可以出现在构造方法,方法,参数,属性,别的注解上。
代码语言:javascript
复制
public interface StudentDao {
    void insert();
}
代码语言:javascript
复制
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
    @Override
    public void insert() {
        System.out.println("StudentDaoImpl");
    }
}
代码语言:javascript
复制
@Service("StudentService")
public class StudentService {
    @Autowired
    StudentDao studentDao;
    public void test(){
        studentDao.insert();
    }
}
代码语言:javascript
复制
//测试类
@Test
public void AnnotationInto(){
    ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
    StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
    studentService.test();
}

@Qualifier(注入)注解

代码语言:javascript
复制
//当有多个实现类对象时,@Autowired和Qualifier联合使用,可以根据名称进行装配
//Qualifier可以指定要执行的实现类对象
代码语言:javascript
复制
public interface StudentDao {
    void insert();
}
代码语言:javascript
复制
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
    @Override
    public void insert() {
        System.out.println("StudentDaoImpl");
    }
}
代码语言:javascript
复制
@Repository("StudentDaoImpl2")
public class StudentDaoImpl2 implements StudentDao{
    @Override
    public void insert() {
        System.out.println("StudentDaoImpl2");
    }
}
代码语言:javascript
复制
//无法自动匹配。“studentdao”类型的bean不止一个。由此可见@Autowired不能在多个相同类型的Bean对象中使用。
代码语言:javascript
复制
@Service("StudentService")
public class StudentService {
    @Autowired
    @Qualifier("StudentDaoImpl2")
    StudentDao studentDao;
    public void test(){
        studentDao.insert();
    }
}
代码语言:javascript
复制
//测试
@Test
public void AnnotationInto(){
    ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
    StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
    studentService.test();
}

@Resource注入注解(推荐)

代码语言:javascript
复制
@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>
代码语言:javascript
复制
public interface StudentDao {
    void insert();
}
代码语言:javascript
复制
@Repository("StudentDaoImpl")
public class StudentDaoImpl implements StudentDao{
    @Override
    public void insert() {
        System.out.println("StudentDaoImpl");
    }
}
代码语言:javascript
复制
@Repository("StudentDaoImpl2")
public class StudentDaoImpl2 implements StudentDao{
    @Override
    public void insert() {
        System.out.println("StudentDaoImpl2");
    }
}
代码语言:javascript
复制
@Service("StudentService")
public class StudentService {
    //@Autowired
    //@Qualifier("StudentDaoImpl2")
    @Resource(name ="StudentDaoImpl2")
    StudentDao studentDao;
    public void test(){
        studentDao.insert();
    }
}
代码语言:javascript
复制
//测试
@Test
public void AnnotationInto(){
    ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
    StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
    studentService.test();
}

Java类代替SpringConfig.xml配置文件(全注解开发)

代码语言:javascript
复制
//创建一个类来代替SpringConfig.xml

@Configuration
//组件扫描
@ComponentScan({"com.spring.bean","com.spring.Dao"})
public class SpringConfig {
}
代码语言:javascript
复制
//测试

 @Test
    public void AnnotationInto(){
        ApplicationContext applicationContext =new AnnotationConfigApplicationContext(SpringConfig.class);
        StudentService studentService = applicationContext.getBean("StudentService", StudentService.class);
        studentService.test();
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring6之Ioc注解的使用
  • 选择性实例化Bean
  • @Value(注入)注解
  • @Autowired(注入)注解
  • @Qualifier(注入)注解
  • @Resource注入注解(推荐)
  • Java类代替SpringConfig.xml配置文件(全注解开发)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档