80.【Spring5】(四)

简介: 80.【Spring5】

3.@Qualifier(value = “”) 需要一起使用

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用

(1).什么是自动装配的环境比较复杂?

比如: 因为我们注解自动装配用的是: ByType这个类型,所以我们在配置的时候不能同时出来两个类型相同的数bean。然而我们又想用,所以就出现了复杂类型的环境。

处理不了这种情况

怎么解决:添加一个注解

实体类

package Com.Jsxs.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class People {
    @Autowired
    @Qualifier(value = "cat1111")
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

配置

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        <context:annotation-config/>
        <bean id="dog11" class="Com.Jsxs.pojo.Dog"/>
        <bean  id="cat11" class="Com.Jsxs.pojo.Cat"/>
        <bean  id="cat1111" class="Com.Jsxs.pojo.Cat"/>
        <bean id="people" class="Com.Jsxs.pojo.People"/>
</beans>

4.@Resource自动装配(先名字后类型)

最大的区别: 就是如果遇到复杂的类型就不用一起写上面两个注解了,只要写上这一个注解就可以产生了。(整合了上面的注解)

@Resource(name = "cat2")
(1).进行测试Resource这个注解
package Com.Jsxs.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class People {
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        <context:annotation-config/>
        <bean id="dog" class="Com.Jsxs.pojo.Dog"/>
        <bean id="cat" class="Com.Jsxs.pojo.Cat"/>
        <bean id="cat2" class="Com.Jsxs.pojo.Cat"/>
        <bean id="people" class="Com.Jsxs.pojo.People"/>
</beans>

5.小结

(1).@Resource和@Autowired的区别和@Qualifier
  1. 都是用来自动装配的,都可以放在属性的字段上
  2. @Autowired先ByType 后 ByName;而且必须这个对象存在
  3. @Resource先ByName 后 ByType;如果两个都找不到就报错
  4. @Qualifier必须要和@Autowired 共同用

(九)、使用注解开发

在Spring4之后,要使用注解开发,必须保证aop的包导入了

使用注解要导入Context约束,增加注解的支持

1.@Component《bean》

(1).搭建环境
(1).指定需要扫描的包,这个包下的注解就会生效
<context:component-scan base-package="Com.Jsxs.pojo"/>
(2).约束
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
xmlns:context="http://www.springframework.org/schema/context"
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--    指定需要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="Com.Jsxs.pojo"/>
<!--    开启注解的驱动-->
    <context:annotation-config/>
</beans>
(2).Component注解

相当于

<bean id="user" class="Com.Jsxs.pojo.User"/>

配置

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--    指定需要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="Com.Jsxs.pojo"/>
<!--    开启注解的驱动-->
    <context:annotation-config/>
</beans>

实体类

package Com.Jsxs.pojo;
import org.springframework.stereotype.Component;
@Component
// <bean id="user" class="Com.Jsxs.pojo.User"/>
public class User {
    public String name="吉士先生";
}

测试

import Com.Jsxs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user",User.class);
        System.out.println(user.name);
    }
}

2.@Value《属性注入》

相当于

<property name="name" value="川川"/>

实体类

package Com.Jsxs.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
// <bean id="user" class="Com.Jsxs.pojo.User"/>
public class User {
    @Value("川川")
    public String name;
}

3.衍生的注解

@Component有几个衍生注解,我们在web开发中,会按照MVC三成架构分层

  1. dao 【@Repository】
  2. service 【@Service】
  3. controller【@Controller】
  4. Component【@Component】
    这四个注解功能都是一样的,都是代表某个类注册到Spring中,装配Bean

4.自动装配

  1. @Autowired先ByType 后 ByName;而且必须这个对象存在
  2. @Resource先ByName 后 ByType;如果两个都找不到就报错
  3. @Qualifier必须要和@Autowired 共同联用

5.作用域

相当于在bean中注入的 scope

@Scope("prototype")
package Com.Jsxs.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
// <bean id="user" class="Com.Jsxs.pojo.User"/>
@Scope("prototype")
public class User {
    @Value("川川")
    public String name;
}

6.小结

xml与注解

  • xml 更加万能,适用于任何场合!维护简单方便
  • 注解 不是自己的类是用不了,维护相对复杂!
    XML与注解最佳实践
  • XML用来管理Bean
  • 注解只负责完成属性的注入;
  • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!--    指定需要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="Com.Jsxs.pojo"/>
<!--    开启注解的驱动-->
    <context:annotation-config/>

7.注解类的全部步骤【】【】【】

xml中:
(1).配置约束
(2).开启注解支持
- 开启代理的注解支持: <aop:aspectj-autoproxy/>
- 开启注解的驱动: <context:annotation-config/>
(3).扫描包
 <context:component-scan base-package="Com.Jsxs"/>
类中: 
(1).这个类由Spring进行托管(bean)
@Component
(2).给类中的属性进行赋值
@Value
(3).设置作用域
@Scope
(4).对类中的复杂属性进行自动装配
@Autowired
(5).这个另类变成切面类
@Aspect 
(6).设置通知,通知里面的内容要是切入点
@Before("execution(* Com.Jsxs.service.UserServiceImp..*(..))")

(十)、使用Java的方式配置Spring

我们现在要完全不使用Spring的cml配置了,全权交给java来做;

JavaConfig 是Spring的一个子项目,在Spring4之后,他成为了一个核心功能。

1.搭建环境

配置文件类

package Com.Jsxs.Config;
import Com.Jsxs.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
/*
    @Configuration: 代表这是一个配置类,和我们之前看的beans.xml一样
 */
@ComponentScan("Com.Jsxs.pojo")   //扫描包
@Import(JsxsConfig2.class)  //
public class JsxsConfig {
    /*
        1.如果开启包扫描,加载配置类以后,就可以通过反射拿到配置类中的对象了
        2.@Bean只写在方法上,返回的是一个对象,但一般不获取已经在容器中的休想
        3.@Bean 可以用于通过方法获取数据库连接池Connection这种对象
     */
    @Bean   //注册一个bean,就相当于我们之前写的bean标签,这个方法的名字相当于bean标签的id属性。方法的返回值就相当于bean中的class属性
    /*
    @Bean 告知Spring这个方法将会返回一个对象,这个对象要注册Spring应用上下文中的bean.通常方法体中包含了最终产生bean实列的逻辑
     */
    public User getUser(){
        return new User();
    }
}

实体类

package Com.Jsxs.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
/*
    @Component: 这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中;表名一个类作为组件类,并告知Spring要为这个类创建bean
 */
public class User {
    @Value("川川")
    private String name;
}

测试类

import Com.Jsxs.Config.JsxsConfig;
import Com.Jsxs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
    public static void main(String[] args) {
//        ACA_C
        /*
        如果我们完全使用了配置类方式去做,我们九只能通过 ACA_C 进行获取
         */
        ApplicationContext context = new AnnotationConfigApplicationContext(JsxsConfig.class);
        User user = context.getBean("getUser", User.class);
        System.out.println(user.getName());
    }
}

import Com.Jsxs.Config.JsxsConfig;
import Com.Jsxs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
    public static void main(String[] args) {
//        ACA_C
        /*
        如果我们完全使用了配置类方式去做,我们九只能通过 ACA_C 进行获取
         */
        ApplicationContext context = new AnnotationConfigApplicationContext(JsxsConfig.class);
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

相关文章
|
7月前
|
Java 容器 Spring
|
Java Spring 容器
spring之HttpInvoker
  一、HttpInvoker是常用的Java同构系统之间方法调用实现方案,是众多Spring项目中的一个子项目。顾名思义,它通过HTTP通信即可实现两个Java系统之间的远程方法调用,使得系统之间的通信如同调用本地方法一般。
2325 0
|
3天前
|
Java 测试技术 数据库连接
Spring常见知识总结
Spring常见知识总结
8 2
|
10天前
|
缓存 前端开发 Java
|
25天前
|
Java 程序员 Maven
|
7月前
|
Java Spring 容器
|
4月前
|
前端开发 Java 开发者
【Spring】 ——初识Spring
【Spring】 ——初识Spring
39 0
|
4月前
|
Java Spring
Spring中那些BeanFactoryPostProcessors详解(二)
Spring中那些BeanFactoryPostProcessors详解(二)
16 0
|
7月前
|
XML Java 数据格式
|
消息中间件 开发框架 NoSQL
回顾Spring
Spring是什么 Spring是一个开源框架,2003 年兴起的一个轻量级的Java 开发框架,作者:Rod Johnson 。 Spring是为了解决企业级应用开发的复杂性而创建的,简化开发。