XML在一些情况下是必须使用的,比如当我们因为各种原因无法修改 Java 代码时。
但若我们可以修改 Java 代码,那么使用注解,会非常方便。
示例1:保留xml配置,但不在其中配置bean
项目结构
.
├── build.gradle
└── src
└── main
├── java
│ └── demo
│ ├── Main.java
│ └── bean
│ ├── Address.java
│ └── Person.java
└── resources
└── beans.xml
我们通过@Component
注解,将 Address、Person 声明为 bean。
Address.java
Address 类内容如下:
package demo.bean;
import org.springframework.stereotype.Component;
@Component
public class Address {
private String province = "上海";
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
'}';
}
}
Person.java
Person 类内容如下:
package demo.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Person {
@Autowired
private Address address; // 使用 @Autowired 自动装配 bean
@Override
public String toString() {
return "Person{" +
"address=" + address +
'}';
}
}
beans.xml
beans.xml 中仅配置component-scan
,用来告诉Spring去扫描该包下面定义的bean,内容如下:
<?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:component-scan base-package="demo.bean" />
</beans>
Main.java
Main 类内容:
package demo;
import demo.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person ltt = (Person) ctx.getBean(Person.class);
System.out.println(ltt);
}
}
执行结果
Main 类运行结果:
Person{address=Address{province='上海'}}
示例2:完全去掉 XML 配置
项目结构
.
├── build.gradle
└── src
└── main
├── java
│ └── demo
│ ├── AppConfig.java
│ ├── Main.java
│ └── bean
│ ├── Address.java
│ └── Person.java
└── resources
相比示例1,这个示例少了 beans.xml ,多了 AppConfig 类。
Address 类和 Person 类内容保持不变。
AppConfig.java
AppConfig 类内容:
package demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "demo.bean")
public class AppConfig {
}
@Configuration
注解用来声明这个是一个配置类,@ComponentScan
用来声明Spring要扫描的包。
Main.java
Main 类内容如下:
package demo;
import demo.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Person ltt = (Person) ctx.getBean(Person.class);
System.out.println(ltt);
}
}
ApplicationContext 使用 AnnotationConfigApplicationContext 生成,参数是上面的 AppConfig 。
执行结果
Main 类运行结果:
Person{address=Address{province='上海'}}