spring的Xml配置
基本配置
首先 maven项目中导入架包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
创建一个实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private String name;
private int age;
}
在resource目录下创建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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="user1" class="com.***.pojo.User"/>
</beans>
创建测试方法对其进行测试
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) applicationContext.getBean("user1");
System.out.println("user1 = " + user1);
}
运行之后输出结果: user1 = User(name=null, age=0)
赋值
修改 applicationContext.xml文件,对实体类进行赋值
<bean name="user1" class="com.***.pojo.User">
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
</bean>
运行之后输出结果: user1 = User(name=张三, age=18)
单例及多例
单例模式,故名思意。在一个配置项中有且只能创建一个实体类对象。其在 new ClassPathXmlApplicationContext(“applicationContext.xml”) 时就已经生成实体类对象。而多例需要在applicationContext.getBean(“user1”) 时创建。
单例
<bean name="user1" class="com.***.pojo.User" scope="singleton">
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
</bean>
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) applicationContext.getBean("user1");
System.out.println("user1 = " + user1);
运行之后输出结果: user1 = User(name=张三, age=18)
多例
<bean name="user1" class="com.***.pojo.User" scope="prototype">
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
</bean>
<bean name="user2" class="com.***.pojo.User" scope="prototype">
<property name="age" value="19"></property>
<property name="name" value="李四"></property>
</bean>
@Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) applicationContext.getBean("user1");
System.out.println("user1 = " + user1);
User user2 = (User) applicationContext.getBean("user2");
System.out.println("user2 = " + user2);
}
运行之后输出结果:
user1 = User(name=张三, age=18)
user2 = User(name=李四, age=19)
- bean:通过配置bean将实体类对象交给Spring容器
- id:给被管理的对象命名
- class:被管理对象的全限定名
- scope(进阶):默认singleton即为单利模式,就是在Spring容器中只存在一个的实例。
- property:通过Set方法进行依赖注入
- 依赖注入常用的两种:1.set方法注入。2.构造函数注入。
- 另外两种(了解)p命名和c命名空间注入:不能直接使用,需导入xml约束。
构造方法赋值
修改 applicationContext.xml
<bean name="user1" class="com.gzp.sp.pojo.User">
<constructor-arg name="name" value="张三" index="0"/>
<constructor-arg name="age" value="18" index="1"/>
</bean>
@Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) applicationContext.getBean("user1");
System.out.println("user1 = " + user1);
}
运行之后输出结果: user1 = User(name=张三, age=18)
引用外部文件 properties
创建properties文件 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc://192.168.1.10:3306/test
jdbc.username=root
jdbc.password=root
修改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
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean name="druid" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>