Spring学习笔记_day01_ioc

本文为博主辛苦总结,但愿本身之后返回来看的时候理解更深入,也但愿能够起到帮助初学者的做用.

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

Spring_day01

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

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

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

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

spring优势

方便解耦,简化开发服务器

  • Spring就是一个大工厂,能够将全部对象建立和依赖关系维护,交给Spring管理

AOP编程的支持session

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

声明式事务的支持app

  • 只须要经过配置就能够完成对事务的管理,而无需手动编程
    方便程序的测试框架

  • Spring对Junit4支持,能够经过注解方便的测试Spring程序
    方便集成各类优秀框架eclipse

  • 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();
    }
}
相关文章
相关标签/搜索