Spring初入门
来源:互联网 发布:wm6.5软件下载 编辑:程序博客网 时间:2024/06/05 00:33
第一天开始学习Spring3.0。
spring通过 ClassPathXmlApplicationContext来读取applicationcontext.xml文件(该文件名字可自定义)。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:annotation-config/>
一目了然,既然我们要让spring帮我们注入这些对象,而不是通过我们自己去一个个new出来,那么我们 就必须在这个bean标签中把所有需要spring帮忙注入的对象的class目录(包名不能省略)告诉spring,然后给他起一个名字,也就是bean包含的name标签,其实也可用id标签,除了name标签支持各种特殊字符之外没其他特殊作用。
然后通过在该xml文件中设置各种bean标签,然后通过注入来帮我们new各种各样的我们所需要的对象.
然后注入有三种方法,setter注入,构造器注入,注解注入。
<bean name="students" class="net.nw.vo.Student">
<constructor-arg index="0" value="ckt"></constructor-arg>
<constructor-arg index="1" value="50"></constructor-arg>
<constructor-arg index="2" value="s30"></constructor-arg> ->>>>>>>>>>>>此处为构造器注入,必须有对应的构造方法,index标签指构造方法中的成员变量的索引位置,也可使用type="java.lang.String"类型标签来区分构造方法中的成员变量。
</bean>
<bean name="studentDAOs" class="net.nw.dao.impl.StudentDAOImpl"></bean>
<bean name="studentServices" class="net.nw.dao.service.StudentService">
<!-- setter注入 -->
<!-- <property name="sDAO" ref="studentDAOs"></property> -->---------->>>>>>>>>setter注入,此处注入的是上面已经设置好的bean,所以直接使用上面bean的name属性的内容就可以注入了,假设注入的是基本类型,那么不使用ref标签,而是使用value标签。(setter注入,顾名思义,必须在所在的类中声明对应的getter/setter方法)
如<property name="id" value="8"></property>
</bean>
</beans>
对于上面的bean注入,默认情况下是单例模式singleton,也就是说不管你取多少个这样的bean,其实都是同一个对象,但是可以通过设置scope属性来改成非单例.
具体<bean id="" class="" scope="prototype"></bean>
-------------------------------------------------------------------------------
wtf,还有一种装配方式叫做自动装配,比如我们上面手动去指定每一个bean里面的property这种方式就叫做手动装配。默认就是手动装配!
具体修改bean标签里面的autowire标签,有两个常用的属性,一个是byName,一个是byType。byName是根据该bean的class文件里面的成员变量的名字去找xml文件里面是否有name属性或者id属性与其相等的bean,如果有,就把他装配进去。
如
<bean name="studentDAOs" class="net.nw.dao.impl.StudentDAOImpl"></bean>
<bean name="studentServices" class="net.nw.dao.service.StudentService" scope="prototype"autowire="byName">
<!-- setter注入 -->
<!-- <property name="sDAO" ref="studentDAOs"></property> -->此时net.nw.dao.service这个包里面的StudentService类有一个成员变量名字叫做studentDAOs的话,同时上面那个粉红色字体的bean的name也叫这个名字,所以这种时候spring就会自动帮我们将上面那个bean装配到这个bean的成员变量中.
byType跟上面差不多,不说了。
</bean>
-------------------------------------------------------------------------------
bean的生命周期,一般默认的话是容器一初始化所有bean都初始化,但是可以通过设置lazy-init=“true”,来设置成当你get该bean的时候这个bean才初始化。这样可以加快容器的启动速度。
-------------------------------------------------------------------------------
init-method="init" destroy-method="destroy"这个是每个bean的初始化与销毁时可注册的方法,参数内容为方法名字。但是暂时不要与scope="prototype"一起用.
-------------------------------------------------------------------------------
下面介绍关于常用的注入方式
@resource
一般添加与set方法上面。
example:private String name;
@Resource
public void setName()
{}
默认这样写的话就是按照byType去匹配xml文件中的bean标签。然后帮我们注入,而不用我们手动去填写xml文件的property标签注入。
如果xml中有两个相同type的bean标签怎么办?
private String name;
@Resource(name="x")
public void setName()
{}
假设xml文件中有两个bean,一个name为x,一个name为y。那么它就会匹配到name=x的那个bean了。
- Spring初入门
- 【Spring】Spring oxm 入门初尝试
- Spring 入门
- Spring 入门
- Spring 入门
- Spring入门
- Spring入门
- Spring入门
- Spring入门
- Spring 入门
- spring 入门
- Spring入门
- Spring入门
- Spring入门
- Spring入门
- spring入门
- Spring入门
- spring 入门
- 人生的第一个Java代码!!!!!!!【大数 a+b】
- android shell cp 文件到/system/xxx
- [ural1519]Formula 1 && 插头DP
- 4-10:二叉树镜像
- 第六届蓝桥杯大赛个人赛省赛(软件类)C++A组 解题报告
- Spring初入门
- PostgreSQL数据类型
- 海量数据处理算法—Bit-Map
- 康托展开
- tar命令的详细解释
- web.xml配置详解
- Gson进行json字符串和对象之间的转化
- 【源码剖析】MemoryPool —— 简单高效的内存池 allocator 实现
- 海量数据处理双层桶