什么是 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);
- }
- }
基本数据类型和字符串 使用 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>
也可以通注解的方式自动注入
在相应的类上加上注解.
@Repository 持久化注解.
@Service 业务层注解
@Controller 控制层注解
@Autowired 自动注入 按照类型帮你自动注入, 如果由多个类型相同的那么就会在按照名称注入.(建议使用这个)
@Resouce 自动注入 按照名称注入, 如果没有相同名称的 bean 那么会按照类型帮你注入. 它可以指定名称来注入
- <?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 属性, 会得到本电脑的用户名
来源: http://www.bubuko.com/infodetail-3186452.html