Spring概述
以下内容仅讲解spring IOC基本使用方法
spring IOC: 依赖注入
spring AOP:管理组件对象,维护对象关系。目的:降低组件耦合度
Spring web MVC:MVC设计:架构一个MVC结构的WEB程序
Spring整合其他技术:JDBC,Mybatis,Hibernate,Struts等。
Spring IOC应用:
以注入的方式应用对象,实现组件解耦
a.管理对象:创建,初始化,释放资源,销毁
b.维护对象关系:采用注入方式建立对象关系 Dependency Injection(DI)依赖注入(依赖注入:set方法注入,构造器注入)
c.搭建SpringIOC开发环境:引入相关jar包;在src中添加ApplicationContext.xml
一.Spring容器—>管理组件及对象关系
1.创建ApplicationContext对象
2.向applicationContext.xml配置
3.利用ApplicationContext对象getBean()
实例化ApplicationContext容器对象—> applicationContext.xml
二.Spring创建Bean对象的控制
1.控制对象创建方式(使用范围)
在元素中使用scope属性控制,scope可以支持singleton和prototype,默认值是singleton。
该组件在Spring容器中只有一个bean对象,ac.getBean(“id")无论调用getBean();多少次都返回同一个对象;
scope为prototype则每次ac.getBean(“id”);返回一个新的对象。
2.指定对象初始化方法
利用元素的init-method指定,当创建bean对象后自动执行init-method方法。
<bean id="e2"
class="SpringIOC.ExampleBean" scope="prototype" init-method="init">
</bean>
3.指定对象销毁方法
利用元素的destroy-method指定。
满足下面条件才有效:
—组件对象为单例模式
—调用AbstractApplicationContext容器对象的close()方法
4.控制单例对象创建时机
在默认情况下,单例对象是Spring容器创建时实例化;可以使用元素的lazy-init=true属性将创建时机推迟到getBean()方法调用时。
public class ExampleBean {
public ExampleBean()
{
System.out.println("--创建ExampleBean--");
}
public void init()
{
System.out.println("init..");
}
public void destroy()
{
System.out.println("destory object...");
}
public void executor(){
System.out.println("调用executor方法");
}
}
<bean id="e1"
class="SpringIOC.ExampleBean" init-method="init" lazy-init="true" destroy-method="destroy">
</bean>
public class TestBean {
public static void main(String[] args)
{
//创建Spring容器对象
String conf = "applicationContext.xml";
AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从spring容器获取e1,e2
ExampleBean e1 = ac.getBean("e1", ExampleBean.class);
e1.executor();
ExampleBean e2 = ac.getBean("e1", ExampleBean.class);
System.out.println(e1 == e2);//true
ExampleBean e3 = ac.getBean("e2", ExampleBean.class);
ExampleBean e4 = ac.getBean("e2", ExampleBean.class);
System.out.println(e3 == e4);//false
ac.close();//释放Spring容器对象,自动调用单例的destory-method
}
}
IOC: Inversion of Control控制反转
改变了对象获取方式,之前编码方式采用new构造方式获取对象;IOC中采用了由容器创建对象之后注入进来使用。只要修改配置就可以改变对象关系。
三.自动装配(自动注入)
1.自动注入(autowire)
用于指定自动注入规则。可以使用byType,byName,constructor等。用于简化注入配置。
使用byType类型匹配注入需要注意,有2个及其以上匹配会出现异常。
2.各种类型信息的注入配置格式
a).注入字符串,数值单个数值
*b).注入bean对象
<bean id="computer" class="SpringIOC.Computer">
<!--信息set注入-->
<property name="cpu" value="i 5"></property>
<property name="hdd" value="索尼"></property>
<property name="mainbord" value="华硕"></property>
</bean>
<bean id="phone" class="SpringIOC.Phone">
<!--构造器注入-->
<constructor-arg index="0" value="高通">
</constructor-arg>
<constructor-arg index="1" value="2G">
</constructor-arg>
</bean>
<bean id="student" class="SpringIOC.Student">
<!--利用set注入computer-->
<property name="c" ref="computer">
</property>
<!--利用set注入phone-->
<property name="p" ref="phone">
</property>
</bean>
<bean id="student1" class="SpringIOC.Student"
autowire="byType">
</bean>
c).注入集合list,set,map,properties
<!--int和list中不允许传null值-->
<bean id="msg" class="SpringIOC.MessageBean">
<property name="name">
<null/>
</property>
<property name="age" value="18">
</property>
<property name="sex">
<null/>
</property>
<property name="birth" value="2017-07-17">
</property>
<property name="friends">
<list>
<value>tom</value>
<value>jack</value>
<!--<value><null/></value>-->
</list>
</property>
<property name="cities">
<set>
<value>hongkong</value>
<value>shanghai</value>
</set>
</property>
<property name="books">
<map>
<entry key="1001" value="Java基础"></entry>
<entry key="1002" value="JavaWeb开发"></entry>
</map>
</property>
<property name="db">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
d).spring表达式注入
#{表达式}
#{id名.属性}或#{id名.key}
如果时对象属性,需要有getXXX方法
#{List对象id[0]}
<!--定义List对象-->
<util:list id="someList">
<value>小树</value>
<value>静汪</value>
</util:list>
<!--定义Set集合-->
<util:set id="someSet">
<value>hongkong</value>
<value>shanghai</value>
</util:set>
<util:map id="someMap">
<entry key="10003" value="Think in Java"></entry>
<entry key="10004" value="Spring"></entry>
</util:map>
<util:properties id="someProps">
<prop key="username">#{dbParams.user}</prop>
<prop key="password">#{dbParams.password}</prop>
<prop key="databases">#{dbParams.databases}</prop>
</util:properties>
<!--读取db.properties形成一个Properties对象-->
<util:properties id="dbParams"
location="classpath:db.properties">
</util:properties>
<bean id="msg1" class="SpringIOC.MessageBean">
<property name="name" value="#{dbParams['123']}">
</property>
<!--<property name="name" value="#{msg.name}">-->
<!--</property>-->
<property name="friends" ref="someList">
</property>
<property name="cities" ref="someSet">
</property>
<property name="books" ref="someMap">
</property>
<property name="db" ref="someProps">
</property>
</bean>
db.properties:
=====
3.利用注解配置应用IOC
在JDK5.0时追加一些新特性
注解:在类定义,方法定义,成员变量定义前面使用,格式为@注解标记名。
a).组件自动扫描
可以按指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,会将组件扫面倒Soring容器。
<context:component-scan base-package="springIOC"/>
@Component //其他组件
@Controller //控制层组件
@Service //业务层组件xxxService
@Repository //数据访问层组件xxxDao
@Named(需要引入第三方标准包)
@Scope控制对象创建,默认单例
@PostConstruct指定init-method
@PreDestroy指定destroy-method
b).注入注解
@Resource:可以在变量定义前或setXXX方法前应用
@AutoWired:可以在变量定义前或setXXX方法前应用
一般使用时,功能等价,都可以实现注入。
如果不存在多个匹配类型,使用@Resurce或@Autowired都可以。
如果存在多个匹配类型,建议按名称注入
@Resource(name=“指定名称”)或
@Autowired
@Qualifier(“p”)
使用建议:set注入建议用@Resource,构造器建议用@Autowired
自己编写的组件建议使用注解配置;框架API只能用XML配置。
spring配置:
<context:component-scan base-package="springIOC"/>
代码:
@Component //扫描ExampleBean组件,默认id=exampleBean
//@Scope("prototype")//等价于<bean scope="">
@Scope("singleton")//等价于<bean scope="">
public class ExampleBean {
@PostConstruct //等价于<bean init-method="">
public void init(){
System.out.println("初始化逻辑");
}
public void execute(){
System.out.println("---执行execute处理方法---");
}
@PreDestroy //scope为singleton并释放容器资源时,等价于<bean destroy-method="">
public void destroy(){
System.out.println("释放资源");
}
}
@Component("exam1")
public class ExampleBean1 {
public void execute(){
System.out.println("---执行exampleBean1的execute---");
}
}
test
public class ExampleBeanTest {
public static void main(String[] args)
{
String conf = "applicationContext.xml";
AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);
ExampleBean e1 = ac.getBean("exampleBean", ExampleBean.class);
e1.execute();
ExampleBean1 e2 = ac.getBean("exam1", ExampleBean1.class);
e2.execute();
ExampleBean ee = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(e1.toString());
System.out.println(ee.toString());
ac.close();
}
}
运行结果:
---执行execute处理方法---
---执行exampleBean1的execute---
springIOC.ExampleBean@548a102f
springIOC.ExampleBean@548a102f
释放资源
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。