JAVA基础篇 Sring中配置方式:JAVA配置,注解配置

1、Java配置类装配bean

既然是通过Java代码来装配bean,是需要我们自己通过配置类来声明我们的bean。我们先通过@Configuration注解来创建一个Spring的配置类,该类中包含了bean的创建细节——

1.1、 导入jar包或引用maven:

spring-aop-5.0.14.RELEASE.jar 点这里

如果是maven工程,代码如下:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>

1.2、 写一个生成Bean的类文件(类似于xml文件的class文件)

/**
* @author  Nical
*/
@Configuration 
//指定该类为spring ioc容器配置类,相当于beans.xml文件
@ComponentScan(basePackages="com.cc.factory2") 
//开启扫描,会去com.cc.config扫描
//@Component @Value @Autowired进行创建bean或注入属性值
public class BeansConfig {
    
    
    @Bean(name="redPig")
    //将方法返回值纳入到spring ioc容器进行管理,相当于 <bean>
    public Pig getRedPig() {
    
    
        RedPig redPig = new RedPig();
        redPig.setAge(12);
        redPig.setName("王二麻子");
        return redPig;
    }
}

1.3、 测试类

/**
* @author  Nical
*/
public class TestConfig {
    
    
    public static void main(String[] args) {
    
    
        AnnotationConfigApplicationContext context= 
                new AnnotationConfigApplicationContext
                (BeansConfig.class);
                //通过Java配置类告诉spring bean工厂生产什么样的bean
        RedPig redPig = (RedPig) context.getBean(RedPig.class);
        //@Bean 用于匹配没有设置name属性的@Bean方法
        System.out.println("redPig==>"+redPig);
        RedPig redPig2 = (RedPig) context.getBean("redPig");
        //@Bean(name="redPig") 用于匹配设置了name属性的@Bean方法
        System.out.println("redPig2==>"+redPig2);
    }
}

2、通过注解配置文件装配bean

2.1、 xml+注解配置 (在xml中开启注解配置):

注意:这里需要额外导入spring-aop的jar包 点这里

在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
    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/>
<!-- spring扫描哪些包下面的类的注解 -->
<context:component-scan base-package="entity"/>
</beans>

2.2、 实体类:

/**
* @author  Nical
*/
@Component
public class User {
    
    
    @Value("张三")
    //默认会调用该类的set方法,如果没有则会报错
    private String name;
    @Value("19")
    private int age;
    @Autowired  
    //按类型来注入,所以扫描的类中必须要有该类型
    private Dog dog;
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public int getAge() {
    
    
        return age;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }
    public Dog getDog() {
    
    
        return dog;
    }
    public void setDog(Dog dog) {
    
    
        this.dog = dog;
    }

2.3、 测试类:

/**
 1. @author  Nical
*/
public class TestAnnotaion {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext
                ("spring-beans-annotation.xml");
        User user = (User) context.getBean("user");
        System.out.println("user===>" + user);
    }
}
  1. @Component 被标示类会被纳入spring ioc容器进行管理,相当于
    < bean>
  2. @Value 为spring中所有管理的该类对象注入基本类型和String属性值
  3. @Autowired 为spring中所有管理的该类对象注入引用类型属性值;默认按类型注入,可以通过@Qualifier(“dog2”)指定注入哪个bean,同时也可以通过bean加入primary=“true” 优先被Autowired注入
  4. @Resource 默认按类型注入,如果指定了name属性,则按bean名称注入

这两种方式没有什么所谓的优劣之分,主要看使用情况,一般来说是这样:

  • 涉及到全局配置的,例如数据库相关配置、MVC相关配置等,就用JAVA配置的方式
  • 涉及到业务配置的,就使用注解方式

猜你喜欢

转载自blog.csdn.net/m0_50217781/article/details/111226184
今日推荐