Spring注解驱动开发(四)

自动装配。配置类

package com.example.paymentdemo.config;

import com.example.paymentdemo.Dao.BookDao;
import org.springframework.context.annotation.*;

/**
 * @author xushuai
 * @date 2022年03月19日 20:50
 */
/**
 *
 * @author xushuai
 * @date 2022/3/19 20:52
 * @param null
 * @return null
 * 自动装配 ;利用DI(依赖注入),完成对IOC容器中各个组件的依赖关系赋值;
 * 1。自动注入 @Autowired
 * BookService{
 * @Autowired   1。优先按照类型去找对应的组件application.getBean(BookDao.class) 找到就赋值
 *              2。如果这个类型的book有多个 ,再将属性的id作为组件的id去容器中查找
 *              3。@Qualifier("bookDao) 使用@Qualifier指定需要装配的组件的id而不是使用属性名
 *              4。默认一定要将属性赋值好,否则就会报错
 *              5。也可以设置不必须@Autowired(required = false)
 *              6.@Primary 让spring自动装配的时候首选装配,默认使用首选的bean,也可以继续使用@qulified指定需要装配的名字
 * 2。Spring还支持使用@Resource(JSR250)和@Inject(JSR330)
 *   @Resource 可以和@Autowired一样实现自动装配功能,默认是按照组件名称进行装配的,没有能支持requierd =false 和@primary
 * @Inject  需要导入javax.inject的包和Autowierd一样
 *     bookdao
 * }
 */
@Configuration
@ComponentScan({"com.example.paymentdemo.Service","com.example.paymentdemo.Dao"})
public class MainConfigOfAutowired {
    @Primary
    @Bean("bookDao2")
    public BookDao bookDao(){
        BookDao bookDao = new BookDao();
        bookDao.setLabel("2");
        return bookDao;
    }
}

代码

package com.example.paymentdemo.Service;

import com.example.paymentdemo.Dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author xushuai
 * @date 2022年03月19日 20:57
 */
@Service
public class BookService {
    @Autowired(required = false)
    @Qualifier("bookDao2")
    private BookDao bookDao2;

    private void init(){
        System.out.println(bookDao2);
    }

    @Override
    public String toString() {
        return "BookService{" +
                "bookDao=" + bookDao2 +
                '}';
    }
}

package com.example.paymentdemo.Dao;

import org.springframework.stereotype.Repository;

/**
 * @author xushuai
 * @date 2022年03月19日 20:58
 */

public class BookDao {
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    private String label ="1";

    @Override
    public String toString() {
        return "BookDao{" +
                "label='" + label + '\'' +
                '}';
    }
}

package com.example.paymentdemo.main;

import com.example.paymentdemo.Dao.BookDao;
import com.example.paymentdemo.Service.BookService;
import com.example.paymentdemo.config.Main;
import com.example.paymentdemo.config.MainConfigOfAutowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.awt.print.Book;

/**
 * @author xushuai
 * @date 2022年03月19日 20:50
 */
public class TestAutowired {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);
        BookDao bean = applicationContext.getBean(BookDao.class);
        System.out.println(bean);
    }
}

@Value

package com.example.paymentdemo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author xushuai
 * @date 2022年03月12日 4:04 下午
 */
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Person {
    //使用@Value赋值
    /**
     *
     * @author xushuai
     * @date 2022/3/19 20:30
     * @param null
     * @return null
     * 1。基本数值
     * 2。spEL; spring的表达式#{}
     * 3.可以写${}     取出配置文件中的值(在运行环境中的值)
     */
    @Value("xushuai")
    private String name;
    @Value("#{20-2}")
    private String age;
    @Value("${person.nickName}")
    private String nickName;
}
配置文件 property
person.nickName="???"
package com.example.paymentdemo.config;

import com.example.paymentdemo.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @author xushuai
 * @date 2022年03月19日 20:22
 */
@Configuration
//使用@PropertySource读取外部配置文件中的k,v保存到运行的环境变量中
@PropertySource(value = {"classpath:/person.properties"})
public class MainConfigPropertyValues {
    @Bean
    public Person person(){
        return new Person();
    }
}

测试类

package com.example.paymentdemo.entity;

import com.example.paymentdemo.config.MainConfigPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;

import java.util.Arrays;

/**
 * @author xushuai
 * @date 2022年03月19日 20:10
 */
public class TestOne {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigPropertyValues.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        Arrays.stream(beanDefinitionNames).forEach(x->{
            System.out.println(x);
        });
        Person person = (Person) applicationContext.getBean("person");
        System.err.println(person);
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("person.nickName");
        System.out.println(property);
        applicationContext.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值