Spring constructor-arg、c、p
来源:互联网 发布:账户数据库设计 编辑:程序博客网 时间:2024/04/29 20:48
constructor-arg
1、新建Fruit.java
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public Fruit(String name, double money) {
super();
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?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-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit">
<constructor-arg index="0" value="苹果" ></constructor-arg>
<constructor-arg index="1" value="100"></constructor-arg>
</bean>
</beans>
初始化方法有几个的话,进行区分可以在constructor-arg下使用typ进行区分
3、编写Test1.java进行测试
package com.mrw.springtest5;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Fruit fruit=(Fruit)context.getBean("fruit");
System.out.println("水果的名称:"+fruit.getName()+" 价格:"+fruit.getMoney());
}
}
4、运行结果:
c
1、与上述constructor-arg一样建立相同的Fruit.java
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?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"
xmlns:c="http://www.springframework.org/schema/c" <!-- 使用c标签必须加入-->
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-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit" c:_1="100" c:_0="苹果"> <!-- 可改为c:money="100" c:name="苹果" -->
</bean>
</beans>
3、与上述constructor-arg一样建立相同的测试类Test1.java
4、运行结果与上述constructor-arg相同
p
1、与上述constructor-arg一样建立相同的Fruit.java
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public Fruit(String name, double money) {
super();
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
2、将Fruit加入到beans.xml中,并为name、money进行初始化
<?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"
xmlns:p="http://www.springframework.org/schema/p"<!-- 使用p标签必须加入-->
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-4.1.xsd">
<bean id="fruit" name="fruit" class="com.mrw.springtest5.Fruit" p:name="苹果" p:money="100">
</bean>
</beans>
3、编写Test1.java进行测试,Test1.java上述constructor-arg相同
并没有出现预期的效果而是抛出异常:org.springframework.beans.factory.BeanCreationException:
改正方法将Fruit.java改为如下:
package com.mrw.springtest5;
public class Fruit {
private String name;
private double money;
public String getName() {
return name;
}
public double getMoney() {
return money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(double money) {
this.money = money;
}
}
现在运行结果就上述constructor-arg相同啦
- Spring constructor-arg、c、p
- spring - constructor-arg
- Spring Ioc 技术(constructor-arg )
- Spring Ioc 技术(constructor-arg )
- spring - constructor-arg 的使用
- spring - constructor-arg 的使用
- Spring框架 constructor-arg注入
- Spring : constructor-arg property 注入
- spring 的applicationContext.xml中的constructor-arg
- Java Spring Tutorial -- constructor-arg标签
- spring - property和constructor-arg的使用
- spring - property和constructor-arg的使用
- Spring解析子元素constructor-arg
- Spring bean 中 constructor-arg属性
- Spring - constructor-arg和property的使用示例
- spring配置constructor-arg就报错,property就通过
- Spring使用xml配置bean,主要是constructor-arg
- spring源码附录(5)解析子元素constructor-arg
- iOS 本地数据持久化
- Arrays.ArrayList 固定长度的List集合
- Codeforces Round #299
- K-Means++的代码
- linux时间函数总结
- Spring constructor-arg、c、p
- ValueError: urls must start with a leading slash
- ionic初步测试-listView效果
- 删除SQL Server 2008遇到的问题
- java跟日期相关的类
- 华为oj等差数列
- Xiinx中关于DDS IP核 的学习
- ACM进阶之道
- Linux防火墙开放端口