Spring学习笔记_day01_ioc

本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用.

转载请注明 出自 : luogg的博客园 谢谢配合!

Spring_day01

spring是一站式的框架,对EE的三层有每一层的解决方案,Web层,业务层,数据访问层.Web层:SpringMVC , 持久层:JDBC Template , 业务层 : Spring的Bean管理

IOC(Inverse of Control) : 反转控制,将对象的创建交由Spring来完成,反射+配置文件实现.

AOP(Aspect Oriented Programming) : 面向切面编程.

IOC思想 : 工厂+反射+配置文件,底层原理就是提供一个工厂Bean,然后提供一个配置文件,把一些类全都配置在配置文件中,通过xml解析获得类的全路径,从而反射获得类的实例.

spring优点

方便解耦,简化开发

  • Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理

AOP编程的支持

  • Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

声明式事务的支持

  • 只需要通过配置就可以完成对事务的管理,而无需手动编程
    方便程序的测试

  • Spring对Junit4支持,可以通过注解方便的测试Spring程序
    方便集成各种优秀框架

  • Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
    降低JavaEE API的使用难度

  • Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

IOC和DI区别

IOC:控制反转,将对象的创建权交给Spring处理.

DI:依赖注入,在Spring创建对象过程中,把对象依赖属性注入到类中.通过property标签.

Eclipse配置XML提示

window->搜索xml catlog->add 找到schame的位置,将复制的路径copy指定位置,选择schame location.

Bean的3中实现方式

  • 1.默认情况通过无参构造方法实现
  • 2.通过静态方法实例化
  • 3.通过实例工厂实例化

Bean标签的其他配置

id和name的区别

id遵守xml的id的约束,保证这个属性值是唯一的,且必须以字母开头,name没有这些要求,

如果bean标签上没有id,那么name可以作为id.

scope属性

scope属性 :

  • singleton :单例的.(默认的值.)
  • prototype :多例的.
  • request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
  • session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
  • globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

实际开发中主要使用singleton,prototype

Bean属性的注入方式

  • 1.通过构造方法注入
  • 2.通过setter方法注入,最常使用,用property标签,name,value表示普通属性,ref表示引用其他的对象.

若通过构造方法注入,那么bean标签中需要使用

<constructor-arg name="name" value="奔驰S400"></constructor-arg>
<constructor-arg name="price" value="1200000"></constructor-arg>

其中,name也可以换成index,对应的构造方法中的参数的位置.

Bean属性注入:通过P名称空间代替property

Spring2.5版本引入了名称空间p.
p:<属性名>="xxx" 引入常量值
p:<属性名>-ref="xxx" 引用其它Bean对象

引入名称空间:(引入p命名空间)

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
       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">

通过p配置普通属性和带有对象的属性

<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="宝马" p:price="400000"/>
<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>

通过SpEL注入属性

Spring3.0提供注入属性方式:

语法:#{表达式}

<bean id="" value="#{表达式}">

<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
		<property name="name" value="#{'大众'}"></property>
		<property name="price" value="#{'120000'}"></property>
</bean>

<bean id="person" class="cn.itcast.spring3.demo5.Person">
		<!--<property name="name" value="#{personInfo.name}"/>-->
<property name="name" value="#{personInfo.showName()}"/>
		<property name="car2" value="#{car2}"/>直接使用别的bean中的对象
</bean>
	
<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
		<property name="name" value="张三"/>
</bean>

集合属性注入

<!-- 集合的注入 -->
	<bean id="collectionBean" class="com.luogg.demo3.CollectionBean">
		<property name="list">
			<list>
				<value>小花</value>
				<value>小李</value>
			</list>
		</property>
		<property name="map">
			<map>
				<entry key="1" value="洛哥"></entry>
				<entry key="2" value="小美"></entry>
			</map>
		</property>
		<property name="set">
			<set>
				<value>呵呵</value>
				<value>哈哈</value>
			</set>
		</property>
	</bean>

配置文件引入的问题

一种写法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml",”bean2.xml”);
二种方法:在xml中通过import标签引入
<import resource="applicationContext2.xml"/>

通过注解装配Bean

Spring2.5 引入使用注解去定义Bean
@Component 描述Spring框架中Bean

Spring的框架中提供了与@Component注解等效的三个注解:

  • @Repository 用于对DAO实现类进行标注
  • @Service 用于对Service实现类进行标注
  • @Controller 用于对Controller实现类进行标注

三个注解为了后续版本进行增强的.

先去包中扫描这些带注解的类
<!-- 去扫描注解 装配的Bean -->
<context:component-scan base-package="com.luogg.demo1"></context:component-scan>

在类头部通过注解标识这个类是Spring加载的Bean

@Service("helloService")
public class HelloService {
	
	public void sayHello(){
		System.out.println("Hello Spring");
	}
}

最后测试

@Test
    public void test1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloService hello = (HelloService) ac.getBean("helloService");
		hello.sayHello();
	}

Bean的属性的注入

普通属性;

    @Value(value="itcast")    
	private String info;    

对象属性:

@Autowired:自动装配默认使用类型注入.
    @Autowired
	private UserDao userDao;

@Autowired
    @Qualifier("userDao")	--- 按名称进行注入.	
    private UserDao userDao;
等价于
    @Resource(name="userDao")
	private UserDao userDao;

Bean的其他属性的配置

配置Bean初始化方法和销毁方法:

  • init-method 和 destroy-method.
    @PostConstruct 初始化
    @PreDestroy 销毁

配置Bean的作用范围:
@Scope

实际开发中使用XML还是注解?

XML:

  • bean管理

注解;

  • 注入属性的时候比较方便.

两种方式结合;一般使用XML注册Bean,使用注解进行属性的注入.

<context:annotation-config/> 在xml中加上这句话可以识别注解
@Autowired
	@Qualifier("orderDao")
	private OrderDao orderDao;

Spring整合Web开发

正常整合Servlet和Spring没有问题的
但是每次执行Servlet的时候加载Spring配置,加载Spring环境.

  • 将加载的信息内容放到ServletContext中.ServletContext对象时全局的对象.服务器启动的时候创建的.在创建ServletContext的时候就加载Spring的环境.
  • ServletContextListener:用于监听ServletContext对象的创建和销毁的.
方法

导入;spring-web-3.2.0.RELEASE.jar
在web.xml中配置:

<!-- 服务启动时候加载spring -->
 <listener>
 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 因为配置文件不在web-info下边,所以需要配置路径 -->
 <context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>classpath:applicationContext.xml</param-value>
 </context-param>

修改程序代码

/*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");*/
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		HelloService hello = (HelloService) ac.getBean("helloService");
		hello.sayHello();

Spring与Junit整合

  • 1.先导入spring-junit包,spring-test-3.2.0.RELEASE.jar
  • 2.在类上标示
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
	@Autowired
	private HelloService helloService;
	@Test
	public void test1(){
		helloService.sayHello();
	}
}
posted @ 2017-05-02 10:19  洛GG  阅读(344)  评论(0编辑  收藏  举报