概述
导包:
实体类(javaBean):
package until;
public class User {
private String name;
private Integer age;
private
Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public User(){
System.out.println("空参构造方法!!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
package until;
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}
工厂类:
package until;
public class UserFactory {
public static User creatUser(){
System.out.println("静态工厂");
return new User();
}
public User creatUser1(){
System.out.println("实例工厂");
return new User();
}
}
利用工厂类注入属性(配置文件):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="until.User"></bean>
<bean name="user1" class="until.UserFactory" factory-method="creatUser"></bean>
<bean name="user2" factory-bean="factory" factory-method="creatUser1"></bean>
<bean name="factory" class="until.UserFactory"></bean>
</beans>
测试类(工厂注入属性测试):
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import until.User;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.fun1();
}
public void fun(){
ApplicationContext ac = new ClassPathXmlApplicationContext("until/applicationcontext.xml");
User u = (User) ac.getBean("user");
System.out.println(u);
}
public void fun1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("until/applicationcontext.xml");
User u = (User) ac.getBean("user1");
System.out.println(u);
}
public void fun2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("until/applicationcontext.xml");
User u = (User) ac.getBean("user2");
System.out.println(u);
}
}
普通注入(配置文件):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="until.User">
<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
</bean>
<bean name="car" class="until.Car">
<property name="name" value="法拉利"></property>
<property name="color" value="红色"></property>
</bean>
</beans>
测试类(普通注入属性)
package injection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import until.User;
public class Demo {
public static void main(String[] args) {
Demo d = new Demo();
d.fun();
}
public void fun(){
ApplicationContext ac = new ClassPathXmlApplicationContext("injection/applicationcontext.xml");
User u = (User) ac.getBean("user");
System.out.println(u);
}
}
最后
以上就是粗暴猎豹为你收集整理的Spring练习(1)的全部内容,希望文章能够帮你解决Spring练习(1)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复