spring可以用来注入属性实现反转控制,把控制权从class文件中脱离出来。
1.导入spring所需的jar包
2、编写一个简单的pojo类。代码如下:
public class SUser {
private String name;
private String age;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void information() {
System.out.println("姓名:"+name+"年龄:"+age+"密码:"+pwd);
}
}
3.编写配置文件applicationContext.xml。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="suser" class="dps.spring.SUser">
<property name="name" value="小王" ></property>
<property name="age" value="22" ></property>
<property name="pwd" value="123" ></property>
</bean>
</beans>
4、编写一个测试类进行测试。代码如下:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//读取配置文件
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
//从spring容器中获取id为suser的bean
SUser user = act.getBean("suser",SUser.class);
user.information();
}
}
spring+strut2框架整合:(实现微信小程序前台后台通信)https://blog.csdn.net/t554984986/article/details/81811679