1. 通过无参构造方法来创建
1)User
pojo包
public class User{
private String name;
public User(){
System.out.println("User的无参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name = " + name);
}
}
2)beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="cn.edu.jxust.pojo.User">
<property name="name" value="隔竹" />
</bean>
</beans>
3)MyTest
import cn.edu.jxust.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
user.show();
}
}
2. 通过有参构造方法来创建
1)UserT
pojo包
package cn.edu.jxust.pojo;
public class UserT {
private String name;
public UserT(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name = " + name);
}
}
2)beans.xml
第一种:根据index参数下标设置
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 第一种方式根据index参数下标设置-->
<!--
<bean id="userT" class="cn.edu.jxust.pojo.UserT" >
<constructor-arg index="0" value="隔竹" />
</bean>
-->
</beans>
第二种:根据参数名字设置
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 第二种根据参数名字设置 -->
<!--<bean id="userT" class="cn.edu.jxust.pojo.UserT" >
<constructor-arg name="name" value="隔竹隔竹" />
</bean>-->
</beans>
第三种:根据参数类型设置
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="cn.edu.jxust.pojo.UserT">
<constructor-arg type="java.lang.String" value="隔竹隔竹隔竹" />
</bean>
</beans>
3)输出结果
import cn.edu.jxust.pojo.User;
import cn.edu.jxust.pojo.UserT;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserT user = (UserT) context.getBean("userT");
user.show();
}
}