小程序三方平台开发: 解析小程序开发的未来趋势和机遇
536
2023-01-19
详解SpringBean基于XML的装配
1.设值注入:通过反射调用setXxx注入属性值
package com.itheima.assemble;
import java.util.List;
public class User {
private String username;
private Integer password;
private List
/**
* 设值注入
* 提供默认空参构造方法 ;
* 为所有属性提供setter方法。
*/
public User() {
super();
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(Integer password) {
this.password = password;
}
public void setList(List
this.list = list;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password +
", list=" + list + "]";
}
}
package com.itheima.assemble;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlBeanAssembleTest {
public static void main(String[] args) {
String xmlPath = "com/itheima/assemble/beans5.xml";KtsgikY
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
// 构造方式输出结果
System.out.println(applicationContext.getBean("user2"));
}
}
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.3.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-4.3.xsd">
2.构造注入:用+其value属性注入属性值
package com.itheima.assemble;
import java.util.List;
public class User {
private String username;
private Integer passwKtsgikYord;
private List
/**
* 用构造注入
* 创建带所有参数的有参构造方法。
*/
public User(String username, Integer password, List
super();
this.username = username;
this.password = password;
this.list = list;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password +
", list=" + list + "]";
}
}
package com.itheima.assemble;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlBeanAssembleTest {
public static void main(String[] args) {
String xmlPath = "com/itheima/assemble/beans5.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
// 构造方式输出结果
System.out.println(applicationContext.getBean("user1"));
}
}
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.3.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-4.3.xsd">
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~