一、目录结构
知识点
1 知识点1-> 2 BeanFactory才是 Spring 容器中的顶层接口,ApplicationContext 是它的子接口。 3 区别:创建对象的时间点不一样。 4 ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。 5 BeanFactory:什么使用什么时候创建对象。 6 知识点2-> 7 ClassPathXmlApplicationContext :从类的根路径下加载配置文件,推荐使用这种. 8 FileSystemXmlApplicationContext :从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。 9 AnnotationConfigApplicationContext:使用注解配置容器对象时,需要使用此类来创建spring容器,用来读取注解. 10 知识点3->bean标签 11 1、作用:用于配置对象让 spring 来创建的。默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。 12 2、属性: 13 id:给对象在容器中提供一个唯一标识。用于获取对象。 14 class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。 15 scope:指定对象的作用范围。 16 * singleton :默认值,单例的. 17 * prototype :多例的. 18 init-method:指定类中的初始化方法名称。 19 destroy-method:指定类中销毁方法名称。 20 3、作用范围和生命周期: 21 单例对象:scope="singleton" 一个应用只有一个对象的实例。它的作用范围就是整个引用。 22 生命周期: 23 对象出生:当应用加载,创建容器时,对象就被创建了。 24 对象活着:只要容器在,对象一直活着。 25 对象死亡:当应用卸载,销毁容器时,对象就被销毁了。 26 多例对象:scope="prototype" 每次访问对象时,都会重新创建对象实例。 27 生命周期: 28 对象出生:当使用对象时,创建新的对象实例。 29 对象活着:只要对象在使用中,就一直活着。 30 对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。 31 4、实例化Bean的三种方式 32 (1)使用默认无参构造函数:<bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl"/> 33 (2)spring管理静态工厂-使用静态工厂的方法创建对象 34 public class StaticFactory { 35 public static IAccountService createAccountService(){ 36 return new AccountServiceImpl(); 37 } 38 } 39 <bean id="accountService" class="cn.bijian.factory.StaticFactory" factory-method="createAccountService"></bean> 40 (3)spring管理实例工厂-使用实例工厂的方法创建对象 41 public class InstanceFactory { 42 public IAccountService createAccountService(){ 43 return new AccountServiceImpl(); 44 } 45 } 46 <bean id="instancFactory" class="cn.bijian.factory.InstanceFactory"></bean> 47 <bean id="accountService" factory-bean="instancFactory" factory-method="createAccountService"></bean>
二、代码
1、AccountDao
1 package cn.bijian.dao; 2 3 public interface AccountDao { 4 /* 5 模拟保存账户 6 */ 7 void saveAccount(); 8 }
2、AccountDaoImpl
1 package cn.bijian.dao.impl; 2 3 import cn.bijian.dao.AccountDao; 4 5 public class AccountDaoImpl implements AccountDao { 6 @Override 7 public void saveAccount() { 8 System.out.println("保存账户成功!"); 9 } 10 }
3、AccountService
1 package cn.bijian.service; 2 3 public interface AccountService { 4 void saveAccount(); 5 }
4、AccountServiceImpl
1 package cn.bijian.service.impl; 2 3 import cn.bijian.dao.AccountDao; 4 import cn.bijian.dao.impl.AccountDaoImpl; 5 import cn.bijian.service.AccountService; 6 7 8 9 public class AccountServiceImpl implements AccountService { 10 private AccountDao accountDao = new AccountDaoImpl(); 11 12 @Override 13 public void saveAccount() { 14 accountDao.saveAccount(); 15 } 16 }
5、AccountServiceImpl2
1 package cn.bijian.service.impl; 2 3 import cn.bijian.service.AccountService; 4 5 import java.util.Date; 6 7 public class AccountServiceImpl2 implements AccountService { 8 private String name; 9 private Integer age; 10 private Date birthday; 11 12 /* 13 1、构造方法注入 14 */ 15 // public AccountServiceImpl2(String name, Integer age, Date birthday) { 16 // this.name = name; 17 // this.age = age; 18 // this.birthday = birthday; 19 // } 20 21 /* 22 2、set方法注入 23 */ 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 public void setBirthday(Date birthday) { 34 this.birthday = birthday; 35 } 36 37 @Override 38 public void saveAccount() { 39 System.out.println(name+"->"+age+"->"+birthday); 40 } 41 }
6、AccountServiceImpl3
1 package cn.bijian.service.impl; 2 3 import cn.bijian.service.AccountService; 4 5 import java.util.*; 6 7 public class AccountServiceImpl3 implements AccountService { 8 private String[] myStrs; 9 private List<String> myLists; 10 private Set<String> mySets; 11 private Map<String,String> myMaps; 12 private Properties myProps; 13 14 /* 15 3、注入集合属性 16 */ 17 18 public void setMyStrs(String[] myStrs) { 19 this.myStrs = myStrs; 20 } 21 22 public void setMyLists(List<String> myLists) { 23 this.myLists = myLists; 24 } 25 26 public void setMySets(Set<String> mySets) { 27 this.mySets = mySets; 28 } 29 30 public void setMyMaps(Map<String, String> myMaps) { 31 this.myMaps = myMaps; 32 } 33 34 public void setMyProps(Properties myProps) { 35 this.myProps = myProps; 36 } 37 38 @Override 39 public void saveAccount() { 40 System.out.println(Arrays.toString(myStrs)); 41 System.out.println(myLists); 42 System.out.println(mySets); 43 System.out.println(myMaps); 44 System.out.println(myProps); 45 } 46 }
7、Client
1 package cn.bijian.ui; 2 3 import cn.bijian.dao.AccountDao; 4 import cn.bijian.service.AccountService; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class Client { 9 public static void main(String[] args) { 10 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 11 AccountDao accountDao = (AccountDao)ac.getBean("accountDao"); 12 System.out.println(accountDao); 13 AccountService accountService = (AccountService)ac.getBean("accountService"); 14 System.out.println(accountService); 15 } 16 }
8、Client2
1 package cn.bijian.ui; 2 3 import cn.bijian.service.AccountService; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class Client2 { 8 public static void main(String[] args) { 9 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 10 AccountService accountService = (AccountService)ac.getBean("accountService"); 11 accountService.saveAccount(); 12 } 13 }
9、bean.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!--入门--> 7 <!-- <bean id="accountDao" class="cn.bijian.dao.impl.AccountDaoImpl"></bean>--> 8 <!-- <bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl"></bean>--> 9 10 <!--第一种,一定保证都有值--> 11 <!-- <bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl2">--> 12 <!-- <constructor-arg name="name" value="张三"></constructor-arg>--> 13 <!-- <constructor-arg name="age" value="25"></constructor-arg>--> 14 <!-- <constructor-arg name="birthday" ref="now"></constructor-arg>--> 15 <!-- </bean>--> 16 <!-- <bean id="now" class="java.util.Date"></bean>--> 17 18 <!--第二种,不一定保证都有值--> 19 <!-- <bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl2">--> 20 <!-- <property name="name" value="张三"></property>--> 21 <!-- <property name="age" value="25"></property>--> 22 <!-- <property name="birthday" ref="now"></property>--> 23 <!-- </bean>--> 24 <!-- <bean id="now" class="java.util.Date"></bean>--> 25 26 <!--第三种,注入集合属性--> 27 <bean id="accountService" class="cn.bijian.service.impl.AccountServiceImpl3"> 28 <property name="myStrs"> 29 <array> 30 <value>AAA</value> 31 <value>BBB</value> 32 <value>CCC</value> 33 </array> 34 </property> 35 36 <property name="myLists"> 37 <list> 38 <value>AAA</value> 39 <value>BBB</value> 40 <value>CCC</value> 41 </list> 42 </property> 43 44 <property name="mySets"> 45 <set> 46 <value>AAA</value> 47 <value>BBB</value> 48 <value>CCC</value> 49 </set> 50 </property> 51 52 <property name="myMaps"> 53 <map> 54 <entry key="testA" value="AAA"></entry> 55 <entry key="testB" value="BBB"></entry> 56 <entry key="testC" value="CCC"></entry> 57 </map> 58 </property> 59 60 <property name="myProps"> 61 <props> 62 <prop key="testA">AAA</prop> 63 <prop key="testB">BBB</prop> 64 <prop key="testC">CCC</prop> 65 </props> 66 </property> 67 </bean> 68 69 </beans>
10、pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>cn.bijian</groupId> 8 <artifactId>Spring_review_1</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <maven.compiler.source>1.7</maven.compiler.source> 14 <maven.compiler.target>1.7</maven.compiler.target> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.mybatis</groupId> 20 <artifactId>mybatis</artifactId> 21 <version>3.5.6</version> 22 </dependency> 23 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>8.0.21</version> 28 </dependency> 29 30 <dependency> 31 <groupId>log4j</groupId> 32 <artifactId>log4j</artifactId> 33 <version>1.2.17</version> 34 </dependency> 35 36 <dependency> 37 <groupId>junit</groupId> 38 <artifactId>junit</artifactId> 39 <version>4.12</version> 40 <scope>test</scope> 41 </dependency> 42 43 <dependency> 44 <groupId>org.apache.tomcat.maven</groupId> 45 <artifactId>tomcat7-maven-plugin</artifactId> 46 <version>2.2</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.slf4j</groupId> 51 <artifactId>slf4j-log4j12</artifactId> 52 <version>1.6.6</version> 53 </dependency> 54 55 <dependency> 56 <groupId>dom4j</groupId> 57 <artifactId>dom4j</artifactId> 58 <version>1.6.1</version> 59 </dependency> 60 61 <dependency> 62 <groupId>jaxen</groupId> 63 <artifactId>jaxen</artifactId> 64 <version>1.1.6</version> 65 </dependency> 66 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-context</artifactId> 70 <version>5.0.3.RELEASE</version> 71 </dependency> 72 </dependencies> 73 74 </project>