标签:bean tps getbean xmla cat contex app setter spring配置
IOC,inversion of control,控制反转。经典的,我们用关键字new去主动创建对象。而将创建对象的功能交给容器,再根据需要获取就称为控制反转。这里的容器称为IOC容器。
1、pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2、bean类
public class User{
private String username;
private String password;
//getter和setter方法省略
}
3、spring配置文件: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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.tj.entity.User">
</beans>
4、使用
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = applicationContext.getBean("user",User.class);
标签:bean tps getbean xmla cat contex app setter spring配置
原文地址:https://www.cnblogs.com/heibaimao123/p/13792777.html