自动装配。配置类
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 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();
}
}