spring简单项目实战
项目路径
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());}}