在Spring 基本概念里我们已经了解了IoC的思想,本篇讨论Spring IoC的两种实现方式和相关配置。
package com.sitech.test1;
public class Custom {
public void print() {
System.out.println("Custom.print()");
}
}
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="custom" class="com.sitech.test1.Custom">bean>
beans>
package com.sitech.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Custom custom = (Custom) applicationContext.getBean("custom");
custom.print();
}
}
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Custom.print()
<bean id="custom" class="com.sitech.test1.Custom">bean>
package com.sitech.test2;
public class CustomStaticFactory {
public static Custom getCustom() {
return new Custom();
}
}
<bean id="custom" class="com.sitech.test2.CustomStaticFactory" factory-method="getCustom">bean>
package com.sitech.test3;
public class CustomFactory {
public Custom getCustom() {
return new Custom();
}
}
<bean id="customFactory" class="com.sitech.test3.CustomFactory">bean>
<bean id="custom" factory-bean="customFactory" factory-method="getCustom">bean>
package com.sitech.test5;
public class Custom {
private String name;
public void setName(String name) {
this.name = name;
}
public void print() {
System.out.println(name);
}
}
id="custom" class="com.sitech.test4.Custom">
<property name="name" value="Jack">property>
package com.sitech.test6;
public class Restaurant {
public void eat() {
System.out.println("Restaurant.eat()");
}
}
package com.sitech.test6;
public class Custom {
private Restaurant restaurant;
public void setRestaurant(Restaurant restaurant) {
this.restaurant = restaurant;
}
public void hungry() {
restaurant.eat();
}
}
<bean id="restaurant" class="com.sitech.test6.Restaurant">bean>
<bean id="custom" class="com.sitech.test6.Custom">
<property name="restaurant" ref="restaurant">property>
bean>
package com.sitech.test4;
public class Custom {
private String name;
Custom(String name) {
this.name = name;
}
public void print() {
System.out.println(name);
}
}
<bean id="custom" class="com.sitech.test4.Custom">
<constructor-arg name="name" value="Jack">constructor-arg>
bean>
package com.sitech.test7;
import java.util.*;
public class Custom {
private String name;
private String[] array;
private List list;
private Map map;
private Properties properties;
public void setName(String name) {
this.name = name;
}
public void setArray(String[] array) {
this.array = array;
}
public void setList(List list) {
this.list = list;
}
public void setMap(Map map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void print() {
System.out.println(name);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println(list);
System.out.println(map);
System.out.println(properties);
}
}
xmlns:p="http://www.springframework.org/schema/p"
id="custom" class="com.sitech.test7.Custom" p:name="Lucy">
<bean id="custom" class="com.sitech.test7.Custom">
<property name="array">
<list>
<value>小明value>
<value>小强value>
<value>小红value>
list>
property>
bean>
<bean id="custom" class="com.sitech.test7.Custom">
<property name="list">
<list>
<value>红楼梦value>
<value>西游记value>
<value>水浒传value>
list>
property>
bean>
<bean id="custom" class="com.sitech.test7.Custom">
<property name="map">
<map>
<entry key="1" value="足球">entry>
<entry key="2" value="篮球">entry>
<entry key="3" value="排球">entry>
map>
property>
bean>
<bean id="custom" class="com.sitech.test7.Custom">
<property name="properties">
<props>
<prop key="1">LOLprop>
<prop key="2">DNFprop>
<prop key="3">CSGOprop>
props>
property>
bean>
package com.sitech.test8;
public class Custom {
private String name;
public void print() {
System.out.println(name);
}
}
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
beans>
<context:component-scan base-package="com.sitech">context:component-scan>
<context:annotation-config>context:annotation-config>
package com.sitech.test9;
import org.springframework.stereotype.Repository;
@Repository(value="customDao")
public class CustomDao {
public void print() {
System.out.println("CustomDao.print()");
}
}
package com.sitech.test9;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value="customService")
public class CustomService {
@Autowired
CustomDao customDao;
public void print() {
customDao.print();
}
}
package com.sitech.test9;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextAOP.xml");
CustomService customService = (CustomService) applicationContext.getBean("customService");
customService.print();
}
}
package com.sitech.test9;
import org.springframework.stereotype.Repository;
@Repository(value="customDao")
public class CustomDao {
public void print() {
System.out.println("CustomDao.print()");
}
}
package com.sitech.test9;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service(value="customService")
public class CustomService {
@Resource(name="customDao")
CustomDao customDao;
public void print() {
customDao.print();
}
}
package com.sitech.test9;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextAOP.xml");
CustomService customService = (CustomService) applicationContext.getBean("customService");
customService.print();
}
}
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.sitech.test10">context:component-scan>
<bean id="customService" class="com.sitech.test10.CustomService">bean>
<bean id="bookDao" class="com.sitech.test10.BookDao">bean>
<bean id="sportDao" class="com.sitech.test10.SportDao">bean>
beans>
package com.sitech.test10;
public class BookDao {
public void read() {
System.out.println(this.getClass().getName() + ".read()");
}
}
package com.sitech.test10;
public class SportDao {
public void run() {
System.out.println(this.getClass().getName() + ".run()");
}
}
package com.sitech.test10;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service(value="customService")
public class CustomService {
@Resource(name="bookDao")
BookDao bookDao;
@Resource(name="sportDao")
SportDao sportDao;
public void print() {
bookDao.read();
sportDao.run();
}
}
package com.sitech.test10;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContextAnno.xml");
CustomService customService = (CustomService) applicationContext.getBean("customService");
customService.print();
}
}