码迷,mamicode.com
首页 > 编程语言 > 详细

Spring的配置用法

时间:2017-07-09 12:26:18      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:schema   通过   com   val   控制   一个   lazy   context   通用   

1、Spring容器类型为:applicationContext.xml(此xml建在src目录下)
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
default-lazy-init="true"><!-- default-lazy-init="true"设置当调用时才加载 -->
</beans>
==================================================================================================================================================
2、容器的实例化:
String str = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(str);
context.getBean("id",className.Class);//获得类的实例,自动调用无参构造函数创建对象
==================================================================================================================================================
3、注入:
<bean id="hello" class="org.great.ben.HelloWorld">
<property name="userName" value="QQQQQQ"></property><!-- 使用setter注入向类中的属性注入值 -->
<!-- 使用构造器注入的方式,index代表构造函数参数的位置,也可使用name属性 -->
<constructor-arg index="0" value="构造器注入"></constructor-arg>
<constructor-arg index="1" value="5"></constructor-arg>
</bean>
==================================================================================================================================================
4、自动装配:
<bean id="refBean" class="org.great.ben.RefBean" autowire="byName"><!-- 使用autowire,自动装配,此处根据属性名装配 -->
<!-- <property name="hello" ref="hello"></property> --><!-- 向类中的属性注入一个对象,此处不使用自动装配,需要设置ref属性关联 -->
</bean>
byName:根据属性名自动装配(如该bean中有stu属性,那么就会查找名为stu的bean定义);
byType:如果容器中存在与指定属性类型相同的bean,将与该属性自动装配(该bean中的属性为另一个bean的类型);
constructor:应用于构造器参数,若在容器中未找到与构造器参数一致的bean会报异常;
autodetect:通过自省机制来决定是使用constructor还是byType
==================================================================================================================================================
5、关闭资源:(关闭资源后才能调用到销毁的方法)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ClassPathXmlApplicationContext cac = (ClassPathXmlApplicationContext) context;
cac.close();
==================================================================================================================================================
6、注解:
1)依赖注入:
@Autowired:可以处理构造器注入和setter注入(构造器注入首选)(按byType进行注入)
@Resource:只能处理setter注入(setter注入首选)(默认按byName注入,可对type进行设置)
--------------------------------------------------------------------------------------------------------------------------------------------------
2)自动扫描标记:(在类上有次标记,可自动扫描进spring容器中进行定义,无需再次在容器中自定义)
@Component 通用注解
@Named 通用注解
@Repository 持久化层组件注解
@Service 业务层组件注解
@Controller 控制层组件注解
@PostConstruct
public void init(){}//初始化回调方法
@PreDestroy
public void destroy(){}//销毁回调方法
--------------------------------------------------------------------------------------------------------------------------------------------------
3)@Value("#{user.userName}") ""中的值可以是基本类型,也可以是从容器中引用来的ID
--------------------------------------------------------------------------------------------------------------------------------------------------
==================================================================================================================================================
7、集合注入:<util:list />、<util:set />、<util:map/>、<util:properties />
1)可使用注解@Value("#{stuList}")将此list的值赋值给bean中的属性(<util:set />、<util:map/>类似)
<util:list id="stuList">
<value>A</value>
<value>B</value>
</util:list>
--------------------------------------------------------------------------------------------------------------------------------------------------
2)使用util:properties读取文件,之后使用#{user.userName}获得文件中属性值
<util:properties id="user" location="classpath:userText.properties"></util:properties>
==================================================================================================================================================

Spring的配置用法

标签:schema   通过   com   val   控制   一个   lazy   context   通用   

原文地址:http://www.cnblogs.com/monkey200804/p/7140584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!