項目路徑?
models
package com.qcby.demo1;import com.qcby.service.UserService;
import com.qcby.service.UserServiceImpl;public class Dfactory {public UserService createUs(){System.out.println("實例化工廠的方式...");return new UserServiceImpl();}
}
package com.qcby.demo1;
import com.qcby.service.UserService;
import com.qcby.service.UserServiceImpl;
/*** 靜態工廠方法模式**/
public class StaticFactory {public static UserService createUs(){System.out.println("通過靜態工廠的方式創建 UserServiceImpl 對象...");// 編寫很多業務邏輯 權限校驗return new UserServiceImpl();}}
package com.qcby.demo2;public class Car {// 名稱private String cname;// 金額private Double money;public Car(String cname, Double money) {this.cname = cname;this.money = money;}public Car() {}@Overridepublic String toString() {return "Car{" +"cname='" + cname + '\'' +", money=" + money +'}';}
}
package com.qcby.demo3;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class CollectionBean {//數組private String[] strs;private List<String> list;private Map<String,String> map;private Properties properties;public String[] getStrs() {return strs;}public void setStrs(String[] strs) {this.strs = strs;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "CollectionBean{" +"strs=" + Arrays.toString(strs) +", list=" + list +", map=" + map +", properties=" + properties +'}';}
}
serviecs
package com.qcby.service;public interface UserService {public void hello();
}
package com.qcby.service;public class UserServiceImpl implements UserService{@Overridepublic void hello() {System.out.println("Hello IOC!!");}
}
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 http://www.springframework.org/schema/beans/spring-beans.xsd"><!--IOC 管理 bean--><bean id="userService" class="com.qcby.service.UserServiceImpl"/><!--實例化 Bean 對象的三種方式--><!--默認是無參數的構造方法(默認方式,基本上使用)-->
<!-- <bean id="us" class="com.qcby.service.UserServiceImpl" />-->
<!-- <!– 靜態工廠方式–>-->
<!-- <bean id="us" class="com.qcby.demo1.StaticFactory" factory-method="createUs" />-->
<!-- <!– 動態工廠方式–>-->
<!-- <bean id="dfactory" class="com.qcby.demo1.Dfactory" />-->
<!-- <bean id="us" factory-bean="dfactory" factory-method="createUs" />--><!--DI:依賴注入--><!--屬性set方法注入值-->
<!-- <bean id="os" class="com.qcby.service.OrderServiceImpl">-->
<!-- <property name="orderDao" ref="od" />-->
<!-- <property name="msg" value="你好" />-->
<!-- <property name="age" value="30" />-->
<!-- </bean>-->
<!-- <bean id="od" class="com.qcby.dao.OrderDaoImpl"></bean>--><!-- 屬性構造方法方式注入--><bean id="car" class="com.qcby.demo2.Car"><constructor-arg name="cname" value="大奔" /><constructor-arg name="money" value="400000" /></bean><!-- 數組,集合(List,Set,Map),Properties等的注入--><!--給集合屬性注入值--><bean id="collectionBean" class="com.qcby.demo3.CollectionBean"><property name="strs"><!--數組注入--><array><value>張三</value><value>李四</value><value>王五</value></array></property><property name="list"><!--List注入--><list><value>熊大</value><value>熊二</value><value>光頭強</value></list></property><property name="map"><!--Map注入--><map><entry key="1" value="張三" /><entry key="2" value="李四" /><entry key="3" value="王五" /></map></property><property name="properties"><!--Properties注入--><props><prop key="username">root</prop><prop key="password">123456</prop></props></property></bean><!-- 主配置文件中包含其他的配置文件:--><import resource="applicationContext2.xml"/><!-- 工廠創建的時候直接加載多個配置文件:ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");-->
</beans>
log4j.properties(自己添加到resource下)
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
test
import com.qcby.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo1 {@Testpublic void run1(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) context.getBean("userService");userService.hello();}
}
import com.qcby.demo3.CollectionBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo3 {@Testpublic void run3(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");CollectionBean collectionBean = (CollectionBean) context.getBean("collectionBean");collectionBean.getList().forEach(System.out::println);collectionBean.getMap().forEach((k,v)->System.out.println(k+"--"+v));collectionBean.getProperties().forEach((k,v)->System.out.println(k+"--"+v));System.out.println(collectionBean.getStrs());}}