一:Spring配置元数据的三种方式
1.基于Xml的配置
这种方式是完全使用xml文件进行配置的
两个实体类:
CPU:
package spring01com; import org.springframework.stereotype.Component; public class InterCpu implements Cpu{ @Override public void run() { // TODO Auto-generated method stub System.out.println("Cpu启动"); } }
Computer:
package spring01com; import javax.annotation.Resource; import org.springframework.stereotype.Component; public class Computer { Cpu cpu; public Cpu getCpu() { return cpu; } public void setCpu(Cpu cpu) { this.cpu = cpu; } public void playGame() { cpu.run(); System.out.println("玩游戏"); } }
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- bean就是java对象由Spring来创建和管理 --> <!-- <bean name="hello" class="net.xyz.bean.Hello"> <property name="name" value="张三"></property> </bean> --> <!-- 团队协作通过import方式来实现的 --> <bean id="Computer" class="spring01com.Computer"> <property name="cpu" ref="Cpu"></property> </bean> <bean id="Cpu" class="spring01com.InterCpu"></bean> </beans>
启动类:
public class Run { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); // ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class); Computer pc=(Computer) ctx.getBean("Computer"); pc.playGame(); } }
2.基于注解的配置
实体类:
CPU:
@Component public class InterCpu implements Cpu{ @Override public void run() { // TODO Auto-generated method stub System.out.println("Cpu启动"); } }
Computer:
@Component("Computer") public class Computer { @Resource Cpu cpu; public Cpu getCpu() { return cpu; } public void setCpu(Cpu cpu) { this.cpu = cpu; } public void playGame() { cpu.run(); System.out.println("玩游戏"); } }
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- bean就是java对象由Spring来创建和管理 --> <!-- <bean name="hello" class="net.xyz.bean.Hello"> <property name="name" value="张三"></property> </bean> --> <context:annotation-config></context:annotation-config> <context:component-scan base-package="spring01com"></context:component-scan> </beans>
启动类:
public class Run { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); // ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class); Computer pc=(Computer) ctx.getBean("Computer"); pc.playGame(); } }
3.基于Java的配置
不再需要配置文件。完全使用注解配置
实体类:
CPU :
@Component public class InterCpu implements Cpu{ @Override public void run() { // TODO Auto-generated method stub System.out.println("Cpu启动"); } }
电脑:
@Component("Computer") public class Computer { @Resource Cpu cpu; public Cpu getCpu() { return cpu; } public void setCpu(Cpu cpu) { this.cpu = cpu; } public void playGame() { cpu.run(); System.out.println("玩游戏"); } }
启动类:
@Configuration @ComponentScan("spring01com") public class Run { public static void main(String[] args) { //ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class); Computer pc=(Computer) ctx.getBean("Computer"); pc.playGame(); } }
二:Bean的实例化
1.通过构造方法实例化
Beans:
public class BeanInstance { public BeanInstance() { } public void show() { System.out.println("我是一个实体"); } }
配置文件:
<?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="beanInstance" class="beans.BeanInstance"></bean> </beans>
实体类:
public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); BeanInstance beanInstance=(BeanInstance) ctx.getBean("beanInstance"); beanInstance.show(); } }
测试类:
public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); BeanInstance beanInstance=(BeanInstance) ctx.getBean("beanInstance"); beanInstance.show(); } }
2.使用静态工厂方法实例化
实体类:
public class BeanInstance { private static BeanInstance beanInstance ; public BeanInstance() { show(); } public void show() { System.out.println("我是一个实体"); } public static BeanInstance create() { if(beanInstance==null) { beanInstance=new BeanInstance(); return beanInstance; }else { return beanInstance; } } }
配置文件:
<?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="beanInstance" class="beans.BeanInstance" factory-method="create"></bean> </beans>
测试类
public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); BeanInstance beanInstance1=(BeanInstance) ctx.getBean("beanInstance"); BeanInstance beanInstance2=(BeanInstance) ctx.getBean("beanInstance"); System.out.println(beanInstance1==beanInstance2); } }
3.使用实例工厂方法实例化
实体类:
public class BeanInstance2 { public void show() { System.out.println("我是bean2"); } }
工厂类:
public class BeanInstance2Factory { public BeanInstance2 create() { return new BeanInstance2(); } }
配置文件:
<bean id="beanInstance2Factory" class="beans.BeanInstance2Factory" ></bean> <bean id="beanInstance2" factory-bean="beanInstance2Factory" factory-method="create"></bean>
测试类:
BeanInstance2 beanInstance3=(BeanInstance2) ctx.getBean("beanInstance2"); beanInstance3.show();
三:Bean的依赖注入
1.构造器注入
CPU类:
public class InterCpu { public void run() { System.out.println("Cpu 开始运行了"); } }
Computer类:
public class Computer { private String grand; private InterCpu cpu; public Computer(String grand, InterCpu cpu) { super(); this.grand = grand; this.cpu = cpu; System.out.println("cpu是"+grand); } }
bean配置文件
<bean id="InterCpu" class="di.InterCpu"></bean> <bean id="Computer" class="di.Computer"> <constructor-arg value="联想"></constructor-arg> <constructor-arg ref="InterCpu"></constructor-arg> </bean>
2.setter注入
computer类:
package di; public class Computer1 { private String grand; private String size; private InterCpu cpu; public String getGrand() { return grand; } public void setGrand(String grand) { this.grand = grand; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public InterCpu getCpu() { return cpu; } public void setCpu(InterCpu cpu) { this.cpu = cpu; } @Override public String toString() { return "Computer1 [grand=" + grand + ", size=" + size + ", cpu=" + cpu + "]"; } }
bean配置文件:
<bean id="Computer1" class="di.Computer1"> <property name="grand" value="thinkpad"></property> <property name="size" value="15"></property> <property name="cpu" ref="InterCpu"></property> </bean>