Bean依赖注入

Bean依赖注入

  1. 手动注入

xml注入:构造方法注入,属性注入

注解注入,(过会讲)

  1. 自动注入

byType:按照类型讲

byName:按照名称

构造方法注入

<beanid="demo1DaoId"class="cn.itcast.g_injection.a_cons.Demo1Dao">

<!--<constructor-arg>标签可以配置构造方法参数列表 index:参数的索引值 value:参数的值type:参数的类型,需要全限定类名。例如:java.lang.String -->

<!--一般情况,index和type将结合使用 -->

<!--ref:references,spring容器中其它bean引用名称 -->

<constructor-argindex="0" value="1234"type="java.lang.String"></constructor-arg>

<constructor-argindex="1" value="1231456"></constructor-arg>

</bean>

属性注入

  1. 属性注入

<beanid="parentId"class="cn.itcast.g_injection.b_prop.Parent">

<propertyname="name" value="设置的名称"></property>

<propertyname="part" ref="partId"></property>

</bean>

<beanid="partId" class="cn.itcast.g_injection.b_prop.Part"/>

  1. 命名空间注入

必须引入命名空间

xmlns:p="http://www.springframework.org/schema/p"

<beanid="parentId2" class="cn.itcast.g_injection.c_P.Parent2"p:name="jack" p:part-ref="partId">

</bean>

<beanid="partId" class="cn.itcast.g_injection.c_P.Part2"/>

SpELI

集合注入

<beanid="parentId4"class="cn.itcast.g_injection.e_collection.Parent4">

<propertyname="datalist">

<list>

<value>刘德来</value>

<value>张爱琴</value>

<value>刘浩</value>

<value>刘颖</value>

<value>刘欢</value>

<value>刘红艳</value>

</list>

</property>

<propertyname="dataSet">

<set>

<value>刘德来</value>

<value>张爱琴</value>

<value>刘浩</value>

<value>刘颖</value>

<value>刘欢</value>

<value>刘红艳</value>

</set>

</property>

<propertyname="dataMap">

<map>

<entrykey="ds" value="屌丝"></entry>

<entry>

<key>

<value>dzd</value>

</key>

<value>屌中屌</value>

</entry>

</map>

</property>

<propertyname="dataProp">

<props>

<propkey="高富帅">嫐</prop>

<propkey="白富美">嬲</prop>

<propkey="女屌丝">窊</prop>

<propkey="男屌丝">摥</prop>

</props>

</property>

</bean>

配置文件

<importresource

装配bean注解

注解:用于取代xml配置文件,简化配置操作。

缺点:必须有源码

@Component组件,用于取代<bean>配置

计算机生成了可选文字: D.2.8 The context schema The tags deal with configuration that relates to plumbing - that is,not usually beans that are important to an end-user but rather beans that do a lot Appl i cati onCont ext context of grunt work in Spring,such as The following snippet references the correct schema so that the tags in the namespace are available to BeanfactoryPostProcessory context you. (?xml version: I. O springframework. org/schema/beans (beans //w-ww. w3. org/2001/XhLSchema—inst_ ance xmlns : cont ext: http://www. springframework. org/schema/cont ext xsi : schemaLocationz http://w-ww. springframework. org/schema/beans http://w-ww. springframework. org/schema/beans/spring—beans. xsd http://vnww. springframework. org/schema/context http://vnww. springframework. org/schema/context/spring—context.xsd) bean definitions here K/ beans)

屏幕剪辑的捕获时间: 2015/11/6 14:53

添加注解:

计算机生成了可选文字: @Component topi cDaoId) public class TopicDao

扫描注解类

<?xmlversion="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

<!--需要扫描注解类-->

<context:component-scanbase-package="cn.itcast.h_annotation.a_hello"></context:component-scan>

</beans>

使用web开发可以使用@Componet,也可以使用提供其他3个注解,这3个注解都是Component的子类。

@Repository,用于标记dao

@Service,用于标记service

@Controllder,用于标记web

功能是相似的

@AutoWired使用注解将一个类需要的另一个自动注入

生命周期

@postconstruct

public void helloInit() {

System.out.println("初始化");

}

@PreDestroy

public voidhelloDestroy() {

System.out.println("销毁");

}

作用域

单例:@Scope("singleton")或者不写

多例:@scope("prototype")

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...