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 ...
随机推荐
- iOS 用命令实现简单的打包过程
`xcode-select --print-path`/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication // 获得打包 ...
- HTTP响应消息code解释
常见HTTP状态(304,200等) 在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Googl ...
- IOS打包脚本
1.xcodebuild clean -project $projectname.xcodeproj -configuration Release -alltargets2.xcodebuild ar ...
- 修改tabbarcontroller选中图片及选中颜色
1.修改选中图片: UITabBarItem* item = [self.tabBarController.tabBar.items objectAtIndex:1]; //从0开始 item.s ...
- 自定义View(3)关于canas.drawText
本文以Canvas类的下面这个函数为基础,它用来在画布上绘制文本. public void drawText(String text, float x, float y, Paint paint) 效 ...
- Android Activity形象描述
Activity就是形象的说就是一个容器,在里面放置各种控件(按钮,文本,复选框等),就形成了软件的界面~ Activity是可见的,如果不加任何控件的话,那么就像Windows中的空白窗体一样 通过 ...
- [POJ2377]Bad Cowtractors(最大生成树,Kruskal)
题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ...
- git文件未改动pull的时候提示冲突
今天在mac下使用git工具,出现一个很奇怪的问题. 先声明当前工作目录是干净的,运行 git status 没有任何文件改动,且没有任何需要push的文件. 我执行 git pull 命令,直接提示 ...
- ubuntu下安装mysql及外网访问设置
这么多年一直是mssql或者Oracle,mysql基本没用过,借着.net即将跨平台之际,也mysql一把.windows安装基本没啥难度,然后就是试了把linux下...结果坑不少,由于linux ...
- [Sciter系列] MFC下的Sciter–4.HTML与图片资源内置
[Sciter系列] MFC下的Sciter–4.HTML与图片资源内置,防止代码泄露. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterFrame程序,以此作 ...