什么是Spring框架?
Spring 是一个开源框架,它就是一个容器,管理类对象的生命周期.
Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
Spring 是一个 IOC(DI) 和 AOP 容器框架.
具体描述Spring:
轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
依赖注入(DI --- dependency injection、IOC):Class A中用到了Class B的对象b,一般情况下,需要在A的代码中显式的new一个B的对象。
采用依赖注入技术之后,A的代码只需要定义一个私有的B对象,不需要直接new来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new出来并注入到A类里的引用中
面向切面编程(AOP --- aspect oriented programming)
容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)
1. Spring依赖注入的方式。
public class Hello { private String name; private int age; private Student student; private List<Student> list; private Map<String,String> map; public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { System.out.println("name"+name); this.name = name; } public void setAge(int age) { this.age = age; } public Hello(String name, int age) { super(); this.name = name; this.age = age; } public Hello(String name) { super(); this.name = name; } public Hello() { } public Hello(String name, int age, Student student) { super(); this.name = name; this.age = age; this.student = student; } public void show() { System.out.println("name:"+name+";age:"+age+"student:"+student); } }
通过set方法来完成依赖注入。
<bean id="hello1" class="com.zhiyou100.hhz.spring.Hello"> <property name="name" value="zs"/> <property name="age" value="2"/> </bean>
通过构造方法来完成依赖注入。
<bean id="hello2" class="com.zhiyou100.hhz.spring.Hello"> <constructor-arg index="0" value="zs"/> </bean>
基本数据类型和字符串 使用value
<?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"> <bean id="hello1" class="com.zhiyou100.hhz.spring.Hello"> <property name="name" value="zs"/> <property name="age" value="2"/> </bean> </beans>
如果是指向另一个对象的引用 使用ref
<?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"> <bean id="hello2" class="com.zhiyou100.hhz.spring.Hello"> <property name="name" value="zs"/> <property name="age" value="2"/> <property name="student" ref="stu"/> </bean> <bean id="stu" class="com.zhiyou100.hhz.spring.Student"> <property name="address" value="beijing"/> </bean> </beans>
如果类对象注入的属性类型为list类型
<?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">
<bean id="hello3" class="com.zhiyou100.hhz.spring.Hello"> <property name="name" value="lisi"/> <property name="age" value="13"/> <property name="student"> <bean class="com.zhiyou100.hhz.spring.Student"> <property name="addr" value="夏威夷"></property> </bean> </property> <property name="list" > <list> <value>马尔代夫</value> <value>巴黎</value> <value>墨西哥</value> </list> </property> </bean>
</beans>
如果类对象注入的属性类型为map类型。
<?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">
<bean id="hello3" class="com.zhiyou100.hhz.spring.Hello"> <property name="name" value="wangwu"/> <property name="age" value="13"/> <property name="student"> <bean class="com.zhiyou100.hhz.spring.Student"> <property name="addr" value="夏威夷"></property> </bean> </property> <property name="list" > <list> <value>马尔代夫</value> <value>巴黎</value> <value>墨西哥</value> </list> </property> <property name="map"> <map> <entry key="liuliu" value="aa"/> <entry key="qiqi" value="bb"/> </map> </property> </bean>
</beans>
2. Bean的作用域.
3. 自动注入。
<?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"> <bean id="userDao" class="com.zhiyou100.hhz.dao.UserDaoImp"/> <bean id="userService" class="com.zhiyou100.hhz.service.UserServiceImp"> <property name="userDao" ref="userDao"/> </bean> <bean id="userContreller" class="com.zhiyou100.hhz.controller.UserController"> <property name="userService" ref="userService"/> </bean> </beans>
也可以通注解的方式自动注入
在相应的类上加上注解.
<?xml version="1.0" encoding="UTF-8"?> <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-4.2.xsd"> <context:property-placeholder location="com.zhiyou100.hhz" /> </beans>
@Repository public class UserDaoImp implements UserDao { @Override public void findById(int id) { System.out.println("=========根据id查找=========="); } }
@Service public class UserServiceImp implements UserService { @Autowired private UserDao userDao; @Override public void queryById(int id) { userDao.findById(id); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
@Controller(value="UserController") public class UserController { @Autowired private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String findById(int id) { userService.queryById(id); return "update"; } }
4.在spring配置文件中引入属性文件
1 <context:property-placeholder location="classpath:my.properties"/> 2 <bean id="users" class="com.zhiyou100.spring.Users"> 4 <property name="name" value="${users.name}"></property> 5 <property name="age" value="${users.age}"></property> 6 <property name="address" value="${users.address}"></property> 7 </bean>
users.name=zzzz
users.age=55
users.address=asdasd
不能使用user.name属性,会得到本电脑的用户名