下面搭建一下纯净版的spring5环境,主要实现的功能是配置文件和注解方式获取bean,代码例子如下:
一、依赖pom.xml中
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
二、带配置文件版
1、配置文件applicationContext.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--编写对应的配置信息-->
<bean class="com.suibibk.spring.entity.User" id="user"/>
<!-- 开启注解 -->
<context:component-scan base-package="com.suibibk.spring"/>
</beans>
注意:若要支持注解模式,头文件里需要有如下内容:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2、bean对象Car和User
User是用配置文件的方式初始化bean,Car是用注解的方式初始化bean。
package com.suibibk.spring.entity;
public class User {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}
}
package com.suibibk.spring.entity;
import org.springframework.stereotype.Component;
@Component
public class Car {
private String carName;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
@Override
public String toString() {
return "Car [carName=" + carName + "]";
}
}
可以看到,Car上有注解@Component
3、测试类
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
//获取上下文对象ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取实例,配置文件
User user = (User)context.getBean("user");
user.setUsername("suibibk.com");
user.setAge(18);
System.out.println(user);
//获取实例,注解
Car car = (Car)context.getBean("car");
car.setCarName("宝马");
System.out.println(car);
}
}
4、结果
User [username=suibibk.com, age=18]
Car [carName=宝马]
三、纯注解版
现在一般都不用配置文件的方式,直接注解即可。
1、配置文件类
@Configuration
@ComponentScan(basePackages = "com.suibibk.spring")
public class SpringConfiguration {
//spring容器初始化时,会调用配置类的无参构造函数
public SpringConfiguration(){
System.out.println("容器启动初始化");
}
}
2、User
@Component
public class User {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User [userName=" + userName + "]";
}
}
User需要在配置文件类的扫描包下面
3、测试类
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
User user = (User) context.getBean("user");
user.setUserName("个人随笔");
System.out.println(user);
}
}
4、运行结果
容器启动初始化
User [userName=个人随笔]
完成。