标签:命名空间 struct java 导入 ons 不能 pre 直接 context
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private int age;
}
p命名空间注入,可以直接注入属性值:相当于property标签的属性注入
要在配置文件导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性值:property-->
<bean id="user" class="com.saxon.pojo.User" p:name="张三" p:age="18"/>
</beans>
测试:
@Test
public void UserTset(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
//User(name=张三, age=18)
要在配置文件导入xml约束
xmlns:c="http://www.springframework.org/schema/c"
配置文件
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--c命名空间注入,构造器注入:constructor-arg-->
<bean id="user2" class="com.dada.pojo.User" c:name="李四" c:age="20"/>
</beans>
测试:
@Test
public void UserTset(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user.xml");
User user = (User) applicationContext.getBean("user2");
System.out.println(user);
}
p命名空间和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
标签:命名空间 struct java 导入 ons 不能 pre 直接 context
原文地址:https://www.cnblogs.com/saxonsong/p/14899540.html