注解式开发以及java类代替xml

8、注解式自动装配

@Autowired

(1)可以添加在属性上也可以添加在set放上上面,set方法可以省略,但是省略的前提是xml配置文件中必须要有bean对象【如果使用了@Data就无需考虑这些问题】

@ToString
public class User implements Serializable {

    private String name;
    @Autowired
    private Student student;
    private Teacher teacher;

}
<bean id="user" class="com.ch.pojo.User"></bean>
<bean id="student" class="com.ch.pojo.Student"></bean>

(2)Autowired中可以设置参数

@Autowired(required = false)
private Student student;

默认为true,如果设置为false,则可以不需要xml配置文件中必须有该bean对象

(3)Autowired是根据类型(byType)进行自动装配的【重点】

(4)如果参数类型较多,或者一致,就可以配合@Qualifier,通过唯一id进行匹配

@Qualifier

(1)一般配合@Autowired唯一定位一个bean对象,参数为对象名(id)

@Autowired(required = false)
@Qualifier("student111")
private Student student;
<bean id="student111" class="com.ch.pojo.Student"></bean>

@Resource

(1)Resource其实就是@Autowired和@Qualifier的一种结合

(2)装配的规则是:先找名字,名字一致再找类型进行匹配【重点】

(3)可以设置名字参数

@Resource(name = "tea")
private Teacher teacher;
<bean id="tea" class="com.ch.pojo.Teacher"></bean>

9、注解式开发

bean == @Component

注意点:

  • 必须添加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
        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:annotation-config></context:annotation-config>
    <!-- 配置扫描 -->
    <context:component-scan base-package="com.ch.pojo"></context:component-scan>

</beans>
@Data
/**
 * @Component
 * 等效于
 * <bean id="user1" class="com.ch.pojo.User1"></bean>
 * 
 * id ==> 类首字母小写
 */
@Component
public class User1 implements Serializable {

    private String name;
    private Student student;
    private Teacher teacher;

}

衍生出来的几个注解【Spring四大注解】

只要标注了该注解的类,就会自动注入到IOC容器中

(1)@Repository:用来标注在Dao(持久层)上

(2)@Service:用来标注在Service(业务层)上

(3)@Controller:用来标注在Controller(控制层)上

(4)@Component:用来标注在其他类上

属性 == @Value

/**
  * @Value("张三")
  * 等效于
  * <property name="name" value="张三"></property>
  */
@Value("张三")
private String name;

作用域 == @Scope

/**
 * @Scope("singleton")
 * 等效于
 * <bean id="user1" class="com.ch.pojo.User1" scope="singleton"></bean>
 */
@Scope("singleton")
public class User1 implements Serializable {
    …………

总结

xml配置和注解:自己写的类用注解,别人写的类用xml 配置文件

10、Java类代替xml

ApplicationConfig.java

//标注是一个配置类
@Configuration
//配置扫描器
@ComponentScan("com.ch.pojo")
//引入其他Java配置文件
@Import(ApplicationConfig1.class)
public class ApplicationConfig {

    /**
     * @Bean : 标注该该方法是一个bean对象
     *
     * 方法名:相当于bean中的id属性
     * 返回值:相当于bean中的class属性
     * return:相当于bean中返回的对象
     */
    @Bean
    public User1 getUser1(){
        return new User1();
    }

}

测试类

@Test
public void Test1(){
    ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
    User1 user1 = (User1) context.getBean("getUser1");
    System.out.println(user1);
}
posted @ 2020-08-13 20:05  itch  阅读(333)  评论(0编辑  收藏  举报