Spring学习笔记(一) Spring基础IOC、AOP
1. 注入类型
a) Spring_0300_IOC_Injection_Type
b) setter(重要)
c) 构造方法(可以忘记)
d) 接口注入(可以忘记)
以下是setter 注入
- <?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-2.5.xsd">
- <bean id="u" class="com.demo.dao.impl.UserDAOImpl"/>
- <bean id="userService" class="com.demo.service.UserService">
- <property name="userDAO" ref="u"/>
- </bean>
- </beans>
- package com.demo.service;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.demo.model.User;
- // Dependency Injection 依赖注入 依赖容器注入的对象 (灵活)
- // Inverse of Control 控制反转 原来具体的实现控制在自己手里,现在控制在容器手里
- public class UserServiceTest {
- @Test
- public void testAdd() throws Exception{
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- UserService service = (UserService)ctx.getBean("userService");
- User u = new User();
- u.setUsername("li");
- u.setPassword("ww");
- service.add(u);
- }
- }
构造方法注入
- public UserService(UserDAO userDAO) {
- super();
- this.userDAO = userDAO;
- }
- <constructor-arg>
- <ref bean="u"/>
- </constructor-arg>
1. id vs. name
a) Spring_0400_IOC_Id_Name
b) name可以用特殊字符
<bean name="u" class="com.demo.dao.impl.UserDAOImpl"/> 把 id 改成name 原因是可以加特殊字符 (不重要)
1. 简单属性的注入(不重要)
a) Spring_0500_IOC_SimpleProperty
b) <property name=… value=….>
- <bean name="u" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="9"></property>
- <property name="daoStatus" value="helloworld"></property>
- </bean>
1. <bean 中的scope属性
a) Spring_0600_IOC_Bean_Scope
b) singleton 单例 (默认)
c) proptotype 每次创建新的对象
- UserService service = (UserService)ctx.getBean("userService");
- UserService service1 = (UserService)ctx.getBean("userService");
- System.out.println(service == service1);
- <bean id="userService" class="com.demo.service.UserService" scope="prototype">
1. 集合注入
a) Spring_0700_IOC_Collections
b) 很少用,不重要!参考程序
- <?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-2.5.xsd">
- <bean name="userDAO" class="com.demo.dao.impl.UserDAOImpl">
- <property name="sets">
- <set>
- <value>1</value>
- <value>2</value>
- </set>
- </property>
- <property name="lists">
- <list>
- <value>1</value>
- <value>2</value>
- <value>3</value>
- </list>
- </property>
- <property name="maps">
- <map>
- <entry key="1" value="1"></entry>
- <entry key="2" value="2"></entry>
- <entry key="3" value="3"></entry>
- <entry key="4" value="4"></entry>
- <entry key="5" value="5"></entry>
- </map>
- </property>
- </bean>
- <bean id="userService" class="com.demo.service.UserService">
- <property name="userDAO" ref="userDAO"/>
- </bean>
- </beans>
1. 自动装配
a) Spring_0800_IOC_AutoWire
b) byName
c) byType
d) 如果所有的bean都用同一种,可以使用beans的属性:default-autowire
2. 生命周期
- <bean id="userDAO1" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="1"></property>
- </bean>
- <bean id="userDAO2" class="com.demo.dao.impl.UserDAOImpl">
- <property name="daoId" value="2"></property>
- </bean>
- <bean id="userService" class="com.demo.service.UserService" scope="prototype" autowire="byName">
- <property name="userDAO" ref="userDAO1"/>
- </bean>
1. 生命周期
a) Spring_0900_IOC_Life_Cycle
b) lazy-init (不重要)
c) init-method destroy-methd 不要和prototype一起用(了解)
1. Annotation第一步:
a) 修改xml文件,参考文档<context:annotation-config />
2. @Autowired
a) 默认按类型by type
b) 如果想用byName,使用@Qulifier
c) 写在private field(第三种注入形式)(不建议,破坏封装)
d) 如果写在set上,@qualifier需要写在参数上
3. @Resource(重要)
a) 加入:j2ee/common-annotations.jar
b) 默认按名称,名称找不到,按类型
c) 可以指定特定名称
d) 推荐使用
e) 不足:如果没有源码,就无法运用annotation,只能使用xml
4. @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
5. @Scope
6. @PostConstruct = init-method; @PreDestroy = destroy-method;
Annotation-based configuration

context.xml 标签自动提示配置
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
- <p>1. Annotation第一步:</p><p>a) 修改xml文件,参考文档<context:annotation-config /></p><p>1. @Autowired</p><p>a) 默认按类型by type</p><p>b) 如果想用byName,使用@Qulifier</p><p>c) 写在private field(第三种注入形式)(不建议,破坏封装)</p><p>d) 如果写在set上,@qualifier需要写在参数上</p>
- 在UserService 类里面
- @Autowired //把和你参数和对应的类型的的bean注入进来 默认的是byName 用的不多,会产生各种问题
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- (@Qualifier("u") // 可以指定 匹配那个名字的Bean 注入到参数里面来
- public void setUserDAO(@Qualifier("u")UserDAO userDAO) {
- this.userDAO = userDAO;
- }
1. @Resource(重要)
a) 加入:j2ee/common-annotations.jar
b) 默认按名称,名称找不到,按类型
c) 可以指定特定名称
d) 推荐使用
e) 不足:如果没有源码,就无法运用annotation,只能使用xml
@Resource //D:\javasoft\ssh\spring-framework-2.5.6\lib\j2ee\common-annotations.jar 包
1. @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
2. @Scope
3. @PostConstruct = init-method; @PreDestroy = destroy-method;
- <context:component-scan base-package="com.demo"></context:component-scan>
- package com.demo.dao.impl;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.model.User;
- @Component("u")
- public class UserDAOImpl implements UserDAO {
- @Override
- public void save(User u) {
- System.out.println("user save");
- }
- }
- package com.demo.service;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.dao.impl.UserDAOImpl;
- import com.demo.model.User;
- // 面向接口或者抽象编程,需要用谁直接在service 里面new谁 实现DAO接口即可
- //面向抽象编程就是 灵活
- @Component("userService")
- public class UserService {
- private UserDAO userDAO;
- public void init() {
- System.out.println("init");
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- @Resource(name="u") //常用
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public void add(User u) {
- this.userDAO.save(u);
- }
- public void destroy() {
- System.out.println("destroy");
- }
- }
Spring_1300_Annotation_Pre_Post_Scope
- package com.demo.service;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Component;
- import com.demo.dao.UserDAO;
- import com.demo.dao.impl.UserDAOImpl;
- import com.demo.model.User;
- // 面向接口或者抽象编程,需要用谁直接在service 里面new谁 实现DAO接口即可
- //面向抽象编程就是 灵活
- @Component("userService")
- public class UserService {
- private UserDAO userDAO;
- @PostConstruct //构造完成之后
- public void init() {
- System.out.println("init");
- }
- public UserDAO getUserDAO() {
- return userDAO;
- }
- @Resource(name="u") //常用
- public void setUserDAO(UserDAO userDAO) {
- this.userDAO = userDAO;
- }
- public void add(User u) {
- this.userDAO.save(u);
- }
- @PreDestroy // 对象销毁之前指定这个方法
- public void destroy() {
- System.out.println("destroy");
- }
- }
Spring学习笔记(一) Spring基础IOC、AOP的更多相关文章
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java架构师之路 Spring学习笔记(一) Spring介绍
前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...
- Spring学习笔记一:基础概念
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774310.html 一:Spring是什么 Spring的主要作用是作为对象的容器. 传统编程中,我们 ...
- Spring学习笔记--面向切面编程(AOP)
什么是AOP AOP(Aspect Oriented Programming),意为面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- Spring学习笔记(4)——IoC学习
IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械式手表 ...
- [Spring学习笔记 5 ] Spring AOP 详解1
知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...
- 1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)
1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/ 选择需要下载的版本 ...
- Spring 学习笔记(二)—— IOC 容器(BeanFactory)
使用Spring IoC容器后,容器会自动对被管理对象进行初始化并完成对象之间的依赖关系的维护,在被管理对象中无须调用Spring的API. 为了实现IoC功能,Spring提供了两个访问接口: or ...
随机推荐
- NC / Netcat - 文件传输
文件传输:将文件从B用户机器传输到A用户机器. 实验环境1: A用户,windows系统,IP:192.168.12.109 B用户,linux系统,IP:192.168.79.3 A用户作为接受传输 ...
- Android Handler传递参数动态更新UI界面demo
package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...
- (二)CSS基础语法
CSS语法规则由两个主要的部分构成:选择器,以及一条或者多条声明. 下面的示意图为您展示了CSS语法结构: 例如: h1{color:red;font-size:14px;} 值得不同写法和单位 其中 ...
- apk反编译(4)Smali代码注入
转自 : http://blog.sina.com.cn/s/blog_5674d18801019i89.html 应用场景 Smali代码注入只能应对函数级别的移植,对于类级别的移植是无能为力的.具 ...
- 新装的win7 64位系统上装了IE11,想调试网页的时候,按F12,工具会出来,但是没法正常使用,出现空白。
Windows专区开了一帖,没人应.这边再开一帖,看看各位遇到过没.如题,新装的win7 64位系统上装了IE11,想调试网页的时候,按F12,工具会出来,但是没法正常使用.尤其是想切换文档模式,只能 ...
- Android开发之ProgressDialog与ProgressBar
ProgressDialog,继承AlertDialog.所以ProgressDialog就是一个在对话框中显示ProgressDialog,并显示进度的文本信息. 并且没有取消和确定按钮,只能通过b ...
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- 英文 数字 不换行 撑破div容器
我们在div等容器 中,如果规定了宽度,并且里面的内容不是全英文或者全数字是OK的,会自动换行,但是如果是全数字或者是全英文,则会撑破容器,如图 解决方法 word-wrap:break-wo ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...