1.Spring JavaConfig
之前的程序配置是需要依赖xml的诶只方式
现在主要使用基于java代码和Annotation注解的方式完成配置。减少配置文件的和配置内容。
基于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stubean" class="com.wangxing.javaconfigdemo1.StudentBean"></bean>
</beans>
1)创建对象
通过Springboot基于Java代码和Annotation注解的方式代替XML的配置文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestDi1 {
@Bean
public StudentBean stubean(){
return new StudentBean();
}
}
标注了@Configuration的java类定义都是一个JavaConfig配置类
标注了@Bean方法,其返回值将作为bean定义注册到Spring的IoC容器,方法名默认为bean定义的id属性值。
2)构造方法注入
//被调用者
public class PersonBean {
public void testPerson(){
System.out.println("PersonBean类的testPerson方法");
}
}
//调用者
public class StudentBean {
//依赖对象
private PersonBean personBean;
//set
public void setPersonBean(PersonBean personBean) {
this.personBean = personBean;
}
public void methodStudent(){
personBean.testPerson();
System.out.println("Student类的methodStudent方法");
}
}
基于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.example.StudentBean"></bean>
<bean id="person" class="com.example.PersonBean">
<!-- 构造注入 -->
<constructor-arg name="studentbean" ref="student"></constructor-arg>
</bean>
</beans>
SpringBoot通过注解去掉XML配置方式。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestDi1 {
/**
* 依赖对象
* @return
*/
@Bean
public StudentBean stubean(){
return new StudentBean("小明",18);
}
/**
* 调用者
* @return
*/
@Bean
public PersonBean perbean(){
return new PersonBean(stubean());
}
}
3)set方法注入
//被调用者
public class A1 {
private String name;
public A1() {
}
public A1(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//调用者
public class B1 {
private A1 a;
public void setA(A1 a) {
this.a = a;
}
public void test(){
System.out.println(a.getName() + "不知道自己在哪~~");
}
}
基于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a1bean" class="com.example.A1"></bean>
<bean id="b1bean" class="com.example.B1">
<!-- 构造注入 -->
<property name="a1" ref="a1bean"></property>
</bean>
</beans>
基于Java代码和Annotation注解的方式取代基于XML的配置方式
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestDi2 {
// 创建被调用创建对象
@Bean
public A1 a1bean(){
return new A1("张晓");
}
// 创建调用者对象
@Bean
public B1 b2bean(){
B1 b1 = new B1();
b1.setA(a1bean());
return b1;
}
}
实例结果:
3)测试注解
在src/test/java
默认包路径:package----Springbootdemo1ApplicationTests[默认的测试类]
@Test
public void testStudent1(){
ApplicationContext ac=new AnnotationConfigApplicationContext(StudentConfig.class);
StudentBean studentBean=ac.getBean("student", StudentBean.class);
studentBean.methodStudent();
}
代替XML的配置方式的常见注解
1.@ComponentScan(basePackages="基础包名称")
【主类上】 配置自动扫描包,将java类上带有@Component("自定义名称"),@Repository("自定义名称"),@Service("自定义名称"),@Controller
注解的java类实例化。
代替了xml配置文件中
<context:component-scan base-package="基础包名称"></context:component-scan>
2.lombok中的注解
@Setter //添加set
@Getter //添加get方法
@ToString //添加toString 默认按照成员属性进行输出
@AllArgsConstructor //构造函数
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter // 为变量提供set方法
@Getter // 为变量提供get方法
@ToString // 重写toString方法
@AllArgsConstructor //提供有参构造方法
public
class OtherZhujieTest {
private String name;
private String password;
private Integer age;
private String address;
}
3.@Value(“${db.username}”)将资源文件[xxxx.properties]中配置的属性值。
public class OtherZhuJieTest2 {
/**
* 代替了xml配置文件中
* <property name="driverClassName" value="${mydriver}"></property>
*/
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Value("${db.url}")
private String url;
}
3. @Import 与 @ImportResource
在 XML 形式的配置中,我们通过 <import resource="XXX.xml"/> 的形式将多个分开的容器配置合到一个配置中,在 JavaConfig 形式的配置中,我们则使用 @Import 这个 Annotation 完成同样目的:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ConfigrationTest.class,ConfigratonTest2.class}) //将config 和config2合并到AppConfig中
public class AppConfig { }