Spring框架的认识一
Posted 初夏的一棵歪脖子树
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架的认识一相关的知识,希望对你有一定的参考价值。
一、
1.dao
1 package com.zhidi.dao; 2 3 import com.zhidi.entity.Emp; 4 5 public class EmpDao { 6 7 public Integer save(Emp emp) 8 { 9 if(emp!=null) 10 { 11 return 12; 12 } 13 return -2; 14 } 15 }
1 package com.zhidi.dao; 2 3 import com.zhidi.entity.User; 4 5 public class UserDao { 6 7 public Integer save(User user) 8 { 9 if(user!=null) 10 { 11 return 112; 12 } 13 return 0; 14 } 15 16 }
2.entity
1 package com.zhidi.entity; 2 3 import java.util.Arrays; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Set; 7 8 public class Collection1 { 9 10 private List<String> list; 11 private Set<Integer> set; 12 private Map<String, Object> map; 13 private String[] str; 14 public List<String> getList() { 15 return list; 16 } 17 public void setList(List<String> list) { 18 this.list = list; 19 } 20 public Set<Integer> getSet() { 21 return set; 22 } 23 public void setSet(Set<Integer> set) { 24 this.set = set; 25 } 26 public Map<String, Object> getMap() { 27 return map; 28 } 29 public void setMap(Map<String, Object> map) { 30 this.map = map; 31 } 32 public String[] getStr() { 33 return str; 34 } 35 public void setStr(String[] str) { 36 this.str = str; 37 } 38 @Override 39 public String toString() { 40 return "Collection [list=" + list + ", set=" + set + ", map=" + map + ", str=" + Arrays.toString(str) + "]"; 41 } 42 43 44 }
1 package com.zhidi.entity; 2 3 public class Emp { 4 5 }
1 package com.zhidi.entity; 2 3 public class User { 4 5 }
3.test
1 package com.zhidi.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.zhidi.dao.UserDao; 7 import com.zhidi.entity.Collection1; 8 import com.zhidi.entity.User; 9 import com.zhidi.service.EmpService; 10 import com.zhidi.service.UserService; 11 import com.zhidi.util.ConnectionPool; 12 13 public class TestSpring { 14 15 public static void main(String[] args) { 16 //创建APPlicationContext对象 17 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 18 //从Spring容器中获得UserDao 19 UserDao userDao = applicationContext.getBean(UserDao.class); 20 int a=userDao.save(new User()); 21 System.out.println(a); 22 23 24 //从Spring容器中获得UserService 25 UserService service= applicationContext.getBean(UserService.class); 26 boolean b=service.save(null); 27 System.out.println(b); 28 29 //从Spring容器中获得EmpService 30 EmpService emp=applicationContext.getBean(EmpService.class); 31 System.out.println(emp.save(null)); 32 33 34 test(); 35 test1(); 36 } 37 38 39 40 public static void test() 41 { 42 ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 43 //注册时确保调用销毁的方法 44 applicationContext.registerShutdownHook(); 45 applicationContext.getBean(ConnectionPool.class); 46 System.out.println(applicationContext.getBean(ConnectionPool.class).toString()); 47 } 48 49 public static void test1() 50 { 51 ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 52 53 applicationContext.getBean(Collection1.class); 54 System.out.println(applicationContext.getBean(Collection1.class).toString()); 55 } 56 57 58 }
4.service
1 package com.zhidi.service; 2 3 import com.zhidi.dao.EmpDao; 4 import com.zhidi.entity.Emp; 5 6 public class EmpService { 7 8 private EmpDao empDao; 9 //Spring通过该方法将依赖对象empDao注入 10 public void setEmpDao(EmpDao empDao) { 11 this.empDao = empDao; 12 } 13 14 public boolean save(Emp emp) 15 { 16 if(empDao.save(emp)>0) 17 { 18 return true; 19 } 20 return false; 21 } 22 }
1 package com.zhidi.service; 2 3 import com.zhidi.dao.UserDao; 4 import com.zhidi.entity.User; 5 6 public class UserService { 7 private UserDao userDao; 8 9 /* 10 * 定义有参的构造方法 11 */ 12 public UserService(UserDao userDao) { 13 this.userDao = userDao; 14 } 15 16 public boolean save(User user) 17 { 18 if(userDao.save(user)>0) 19 { 20 return true; 21 } 22 return false; 23 } 24 }
5.util
1 package com.zhidi.util; 2 3 4 5 public class ConnectionPool { 6 7 private String userName; 8 private Integer age; 9 private String sex; 10 private String birthday; 11 12 13 public String getUserName() { 14 return userName; 15 } 16 17 public void setUserName(String userName) { 18 this.userName = userName; 19 } 20 21 public Integer getAge() { 22 return age; 23 } 24 25 public void setAge(Integer age) { 26 this.age = age; 27 } 28 29 public String getSex() { 30 return sex; 31 } 32 33 public void setSex(String sex) { 34 this.sex = sex; 35 } 36 37 public String getBirthday() { 38 return birthday; 39 } 40 41 public void setBirthday(String birthday) { 42 this.birthday = birthday; 43 } 44 45 public void init() 46 { 47 System.out.println("初始化"); 48 } 49 50 public void destroy() 51 { 52 System.out.println("销毁"); 53 } 54 55 @Override 56 public String toString() { 57 return "ConnectionPool [userName=" + userName + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday 58 + "]"; 59 } 60 61 62 }
1 package com.zhidi.util; 2 3 public class SingleTon { 4 5 private static SingleTon singleTon; 6 private SingleTon(){ 7 8 } 9 10 public static SingleTon getInstance() 11 { 12 if(singleTon==null) 13 { 14 singleTon=new SingleTon(); 15 16 } 17 18 return singleTon; 19 } 20 21 }
6.properties
1 #userName=zsdfghj 2 age=22 3 sex="\\u7537" 4 birthday="1993.4.7" 5 name=\\u4E8C
7.applicationContext.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 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> 7 <!-- 读取外部配置 --> 8 <context:property-placeholder location="info.properties" /> 9 <!-- 将自定义的UserDao交给Spring管理 --> 10 <bean name="userDao" class="com.zhidi.dao.UserDao" /> 11 <!-- 通过有参方法注入依赖对象 --> 12 <bean class="com.zhidi.service.UserService"> 13 <constructor-arg ref="userDao" /> 14 </bean> 15 16 <!-- 将自定义的EmpDao交给Spring管理 scope默认为singleton,只实体化一次,而prototype每次都实例化 --> 17 <bean name="empDao" class="com.zhidi.dao.EmpDao" scope="prototype" /> 18 19 <!--将empDao通过set注入给EmpService --> 20 <bean class="com.zhidi.service.EmpService"> 21 <property name="empDao" ref="empDao" /> 22 </bean> 23 24 <!--JavaBean生命周期的方法 --> 25 <!-- <bean class="com.zhidi.util.ConnectionPool" init-method="init" destroy-method="destroy"/> --> 26 27 <!-- 普通属性的装配 --> 28 <bean class="com.zhidi.util.ConnectionPool" scope="singleton" 29 init-method="init"> 30 <property name="userName" value="${name}" /> 31 <property name="age" value="${age}" /> 32 <property name="sex" value="${sex}" /> 33 <property name="birthday" value="${birthday}" /> 34 </bean> 35 36 <!-- 集合属性的装配 --> 37 <bean class="com.zhidi.entity.Collection1"> 38 <!-- list集合 --> 39 <property name="list"> 40 <list> 41 <value>二蛋</value> 42 <value>三狗子</value> 43 </list> 44 </property> 45 <!-- set集合 --> 46 <property name="set"> 47 <set> 48 <value>1</value> 49 <value>2</value> 50 </set> 51 </property> 52 <!--Map集合 --> 53 <property name="map"> 54 <map> 55 <entry key="name1" value="丹丹"></entry> 56 <entry key="name2" value="蛋蛋"></entry> 57 </map> 58 </property> 59 <!-- 数组 --> 60 <property name="str"> 61 <array> 62 <value>二狗蛋</value> 63 <value>逗比猫</value> 64 </array> 65 </property> 66 </bean> 67 </beans>
二、
1.dao
1 package com.zhidi.dao; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.context.annotation.Primary; 6 7 import com.zhidi.entity.Emp; 8 9 public class EmpDao { 10 /* 11 * //@Resource 默认byName注入 12 * 13 * @Resource //指定优先被装配的对象 14 * 15 * @Primary 16 */ 17 public Integer save(Emp emp) { 18 if (emp != null) { 19 return 1; 20 } 21 return -1; 22 } 23 }
1 package com.zhidi.dao; 2 3 import javax.annotation.PostConstruct; 4 import javax.annotation.PreDestroy; 5 //生命周期注解引入 6 public class LifeDao { 7 8 @PostConstruct 9 public void init() 10 { 11 System.out.println("初始化开启"); 12 } 13 14 @PreDestroy 15 public void destroy() 16 { 17 System.out.println("销毁"); 18 } 19 }
1 package com.zhidi.dao; 2 3 import org.springframework.stereotype.Component; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.stereotype.Repository; 6 import org.springframework.stereotype.Service; 7 8 import com.zhidi.entity.User; 9 @Component 10 @Service 11 @Repository 12 @Controller 13 public class UserDao { 14 15 public Integer save(User user) 16 { 17 if(user!=null) 18 { 19 return 1; 20 } 21 return -1; 22 } 23 24 }
2.entity
1 package com.zhidi.entity; 2 3 public class Emp { 4 5 }
1 package com.zhidi.entity; 2 3 public class User { 4 5 }
3.service
1 package com.zhidi.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 6 import com.zhidi.entity.Emp; 7 8 public 认识spring框架