作用:简化配置。
使用p命名空间注入的前提条件包括两个:
● 第一:在XML头部信息中添加p命名空间的配置信息:xmlns:p=“http://www.springframework.org/schema/p”
● 第二:p命名空间注入是基于setter方法的,所以需要对应的属性提供setter方法。
Customer.java
package com.w.spring6.bean;
public class Customer {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
spring-p.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customerBean" class="com.w.spring6.bean.Customer" p:name="zhangsan" p:age="20"/>
beans>
测试程序:
@Test
public void testP(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-p.xml");
Customer customerBean = applicationContext.getBean("customerBean", Customer.class);
System.out.println(customerBean);
}
使用util命名空间可以让配置复用。
使用util命名空间的前提是:在spring配置文件头部添加配置信息。如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
MyDataSource1.java
package com.w.spring6.jdbc;
import java.util.Properties;
public class MyDataSource1 {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "MyDataSource1{" +
"properties=" + properties +
'}';
}
}
MyDataSource2.java
package com.w.spring6.jdbc;
import java.util.Properties;
public class MyDataSource2 {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "MyDataSource2{" +
"properties=" + properties +
'}';
}
}
spring-util.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<util:properties id="prop">
<prop key="driver">com.mysql.cj.jdbc.Driverprop>
<prop key="url">jdbc:mysql://localhost:3306/springprop>
<prop key="username">rootprop>
<prop key="password">123456prop>
util:properties>
<bean id="dataSource1" class="com.w.spring6.jdbc.MyDataSource1">
<property name="properties" ref="prop"/>
bean>
<bean id="dataSource2" class="com.w.spring6.jdbc.MyDataSource2">
<property name="properties" ref="prop"/>
bean>
beans>
测试程序:
@Test
public void testUtil(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-util.xml");
MyDataSource1 dataSource1 = applicationContext.getBean("dataSource1", MyDataSource1.class);
System.out.println(dataSource1);
MyDataSource2 dataSource2 = applicationContext.getBean("dataSource2", MyDataSource2.class);
System.out.println(dataSource2);
}
Spring还可以完成自动化的注入,自动化注入又被称为自动装配。它可以根据名字进行自动装配,也可以根据类型进行自动装配。
UserDao.java
package com.w.spring6.dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserDao {
private static final Logger logger= LoggerFactory.getLogger(UserDao.class);
public void insert(){
//System.out.println("数据库正在保存信息");
logger.info("数据库正在保存信息");
}
}
VipDao.java
package com.w.spring6.dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VipDao {
private static final Logger logger= LoggerFactory.getLogger(VipDao.class);
public void insert(){
logger.info("数据库正在保存vip信息");
}
}
UserService.java
package com.w.spring6.service;
import com.w.spring6.dao.UserDao;
import com.w.spring6.dao.VipDao;
public class UserService {
private UserDao userDao;
private VipDao vipDao;
// public void setAbc(VipDao vipDao){
// this.vipDao=vipDao;
// }
public void setVipDao(VipDao vipDao){
this.vipDao=vipDao;
}
//set注入的话,必须提供一个set方法
//spring容器会调用这个set方法,来给userDao赋值
/* //自己写的不符合javabean规范
public void setMySQLUserDao(UserDao xyz){
this.userDao=xyz;
}*/
//idea自动生成的符合javabean规范
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void saveUser(){
userDao.insert();
vipDao.insert();
}
}
spring-autowire.xml
<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="userService" class="com.w.spring6.service.UserService" autowire="byName"/>
<bean id="userDao" class="com.w.spring6.dao.UserDao"/>
<bean id="vipDao" class="com.w.spring6.dao.VipDao"/>
beans>
测试程序:
@Test
public void testAutowireByName(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-autowire.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.saveUser();
}
在类路径下新建jdbc.properties文件,并配置信息
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
username=root
password=123456
在spring配置文件中引入context命名空间,配置使用jdbc.properties文件
<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="jdbc.properties"/>
<bean id="dataSource" class="com.w.spring6.jdbc.MyDataSource">
<property name="driver" value="${jdbc.driverClass}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
beans>