Spring IOC related configuration use

                               Spring IOC related configuration usage

 

Configuration file format XML format configuration

<beans>

  <importresource="spring-res.xml"/> 

  <beanid="aopImpl"class=" com.dy.AopInterfaceImpl"></bean>  

</beans>

import is used to import bean definitions of other configuration files. This is to load multiple configuration files, and these configuration files can also be constructed

An array (new String[] {"config1.xml", config2.xml}) passed to the ApplicationContext implementation to load multiple configuration files .

The centralized form of beans is described as follows

1. Only configure the import classpath

<bean class=”com.dy.AopImpl”/>

The IOC container will automatically generate a default bean identifier. When calling, you need to obtain the bean through the interface "T getBean(Class<T> requiredType)"

2. Specify the ID configuration

<bean id="chinese" class="com.action.Chinese" />

3. Specify the name configuration, the name is the identifier and must be unique.

<bean name="chinese" class="com.action.Chinese" />

4. Specify ID, name, ID is an identifier, name is an alias, (other defined classes cannot be repeated, and must be guaranteed to be unique.)

<bean id=chinese name="chinese" class="com.action.Chinese" />

5. Specify multiple names, multiple names are separated by ",", ";", " ", the first one is used as an identifier, the others (chinese1,chinese2,chinese3) are aliases, all identifiers must also be in Unique in the Ioc container

<bean name="chinese,chinese1,chinese2,chinese3" class="com.action.Chinese" />

6. Use the <alias> tag to specify an alias, the alias must also be unique in the IoC container

<beanid="chinese1"name="chinese"class="main.java.com.action.Chinese"/>     

<aliasname="chinese"alias="chineseA"/>  

Instantiation of JavaBeans

1. Use the constructor to instantiate the Bean.

Use an empty constructor to define, in this way, the class specified by the class attribute must have an empty constructor

<bean  id="chinese1" name="chinese" class="main.java.com.action.Chinese" />

  There is a parameter constructor to define, use the <constructor-arg> tag to specify the constructor parameter value, where index represents the position, value represents the constant value,

 You can also specify a reference, which uses ref to refer to another bean definition.

<bean  id="chinese1" name="chinese" class="main.java.com.action.Chinese" />

    <!-- Specify constructor parameters -->  

    <constructor-arg index="0" value="中国"/>  

</bean>

2. Instantiate beans in static factory mode

<beanid="chinese1"name="chinese"class="com.action.Chinese">     

</bean>

<bean name="chineseFactory" class="com.action.ChineseFactory factory-method="newChinese">

<constructor-arg index="0" value="kkk"></constructor-arg>

</bean>

context = new FileSystemXmlApplicationContext("bean.xml");

Chinese chinese = (Chinese) context.getBean("chineseFactory");

chinese.speak();

 

3.使用实例工厂方法实例化Bean.

<bean name="chineseFactory" class="com.action.ChineseFactory" />

<bean id="chinese" factory-bean="chineseFactory" factory-method="newChinese">

<constructor-arg index="0" value="mmm"></constructor-arg>

</bean>

 

其中 ChineseFactory 实例化如下

public  Chinese newChinese(String name){

  return new Chinese(name);

  }

调用如下

context = new FileSystemXmlApplicationContext("bean.xml");

        Chinese chinese = (Chinese) context.getBean("chinese");

        chinese.speak();

 

Spring注解形式

 

用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册<context:component-scan base-package=”扫描包的路径”/>

 

如:在base-package指明一个包,可以指定多个包

 

  <context:component-scan base-package="com.action,com.service.impl"/>

1.@Component

 
是所有受Spring 管理组件的通用形式是不好确定功能类型的时候标示

2.@Controller

对应表现层的Bean,也就是Action,可以用@Scope注解,@Scope("prototype")表示将Action的范围声明为原型

例如:

@Controller

@Scope("prototype")

Pulbic class PersonAction{}

3.Service

对应的是业务层Bean

4.Repository

对应数据访问层Bean

另外对常用的一些注解做一个分析区别

在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入。虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区 别的。首先来看一下:

 a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;

 b。@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;

 c。@Resource注解是又J2EE提供,而@Autowired是由spring提供,故减少系统对spring的依赖建议使用  

       @Resource的方式;

d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上

java代码中可以使用@Autowire或者@Resource注解方式进行装配,这两个注解的区别是:
@Autowire 默认按照类型装配,默认情况下它要求依赖对象必须存在如果允许为null,可以设置它required属性为false,如果我们想使用按照名称装配,可 以结合@Qualifier注解一起使用;


@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属 性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找 依赖对象.

Note: If the name attribute is not specified, and the dependent object cannot be found according to the default name, it will fall back to assembly by type, but once the name attribute is specified, it can only be assembled by name

 

<!--EndFragment-->

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326619643&siteId=291194637