首页 > 代码库 > spring2
spring2
依赖注入两种方式: 构造函数和属性
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
public class ExampleBean {
// Number of years to calculate the Ultimate Answer
private int years;
// The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value=http://www.mamicode.com/"7500000"/>
<constructor-arg type="java.lang.String" value=http://www.mamicode.com/"42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value=http://www.mamicode.com/"7500000"/>
<constructor-arg index="1" value=http://www.mamicode.com/"42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value=http://www.mamicode.com/"7500000"/>
<constructor-arg name="ultimateanswer" value=http://www.mamicode.com/"42"/>
</bean>
注意使用名称匹配,必须保证你的代码在debug 模式下编译了,否则spring 找不到名称。
还有另一种解决办法,就是使用java 注解:如下
package examples;
public class ExampleBean {
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
构造函数 依赖多用于强制性的配置。
set 方法 多用于 可选择的属性配置。
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
引用ref
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property>
<!-- setter injection using the neater ref attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value=http://www.mamicode.com/"1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
-----------------------------------常用配置
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-
method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"/>
<property name="url" value=http://www.mamicode.com/"jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value=http://www.mamicode.com/"root"/>
<property name="password" value=http://www.mamicode.com/"masterkaoli"/>
</bean>
p-namespace:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="masterkaoli"/>
</beans>
---------------------
<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>
------------引用父亲bean
<bean id="accountService" <-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>
-----------内嵌bean
<bean id="outer" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean
inline -->
<property name="target">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value=http://www.mamicode.com/"Fiona Apple"/>
<property name="age" value=http://www.mamicode.com/"25"/>
</bean>
</property>
</bean>
----------------------集合bean
<list/>, <set/>, <map/>, and <props/>元素设置 Java 集合类型 List, Set, Map, and Properties
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value=http://www.mamicode.com/"just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
map的或者value set 的value 还可以在
是下面这些元素
bean | ref | idref | list | set | map | props | value | null
合并映射
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the child collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>
你知道的,child 的adminEmails 属性里包含了 父子 两个的属性集合。
强类型映射
public class Foo {
private Map<String, Float> accounts;
public void setAccounts(Map<String, Float> accounts) {
this.accounts = accounts;
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<property name="accounts">
<map>
<entry key="one" value=http://www.mamicode.com/"9.99"/>
<entry key="two" value=http://www.mamicode.com/"2.75"/>
<entry key="six" value=http://www.mamicode.com/"3.99"/>
</map>
</property>
</bean>
</beans>
-----------------------空
<bean class="ExampleBean">
<property name="email" value=http://www.mamicode.com/""/>
</bean>
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
------p-namespace
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value=http://www.mamicode.com/"foo@bar.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
</beans>
---c_namespace
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:c= "http://www.springframework.org/schema/c"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "bar" class="x.y.Bar"/>
<bean id= "baz" class="x.y.Baz"/>
<!-- traditional declaration -->
<bean id= "foo" class="x.y.Foo">
<constructor-arg ref= "bar"/>
<constructor-arg ref= "baz"/>
<constructor-arg value= http://www.mamicode.com/"foo@bar.com"/>
</bean>
<!-- c-namespace declaration -->
<bean id= "foo" class="x.y.Foo" c:bar-ref= "bar" c:baz-ref= "baz" c:email= "foo@bar.com"/>
<bean id="foo" class="x.y.Foo" c:_0-ref= "bar" c:_1-ref= "baz"/>
</beans>
---depend-on bean初始化前,被依赖的bean 先初始化。
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
---懒加载(容器启动时候,不愿意生成实例)
bean 级别
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>
容器级别
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
自动装配
byName byType no constructor
autowire-candidate false
-------------lookup method
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean>
<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>
-----repace
<bean id="myValueCalculator" class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
<replaced-method name="computeValue" replacer="replacementComputeValue">
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
public class ExampleBean {
// Number of years to calculate the Ultimate Answer
private int years;
// The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value=http://www.mamicode.com/"7500000"/>
<constructor-arg type="java.lang.String" value=http://www.mamicode.com/"42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value=http://www.mamicode.com/"7500000"/>
<constructor-arg index="1" value=http://www.mamicode.com/"42"/>
</bean>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value=http://www.mamicode.com/"7500000"/>
<constructor-arg name="ultimateanswer" value=http://www.mamicode.com/"42"/>
</bean>
注意使用名称匹配,必须保证你的代码在debug 模式下编译了,否则spring 找不到名称。
还有另一种解决办法,就是使用java 注解:如下
package examples;
public class ExampleBean {
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
构造函数 依赖多用于强制性的配置。
set 方法 多用于 可选择的属性配置。
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
引用ref
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property>
<!-- setter injection using the neater ref attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value=http://www.mamicode.com/"1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
-----------------------------------常用配置
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-
method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"/>
<property name="url" value=http://www.mamicode.com/"jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value=http://www.mamicode.com/"root"/>
<property name="password" value=http://www.mamicode.com/"masterkaoli"/>
</bean>
p-namespace:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="masterkaoli"/>
</beans>
---------------------
<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>
------------引用父亲bean
<bean id="accountService" <-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>
-----------内嵌bean
<bean id="outer" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean
inline -->
<property name="target">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value=http://www.mamicode.com/"Fiona Apple"/>
<property name="age" value=http://www.mamicode.com/"25"/>
</bean>
</property>
</bean>
----------------------集合bean
<list/>, <set/>, <map/>, and <props/>元素设置 Java 集合类型 List, Set, Map, and Properties
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value=http://www.mamicode.com/"just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
map的或者value set 的value 还可以在
是下面这些元素
bean | ref | idref | list | set | map | props | value | null
合并映射
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the child collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>
你知道的,child 的adminEmails 属性里包含了 父子 两个的属性集合。
强类型映射
public class Foo {
private Map<String, Float> accounts;
public void setAccounts(Map<String, Float> accounts) {
this.accounts = accounts;
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<property name="accounts">
<map>
<entry key="one" value=http://www.mamicode.com/"9.99"/>
<entry key="two" value=http://www.mamicode.com/"2.75"/>
<entry key="six" value=http://www.mamicode.com/"3.99"/>
</map>
</property>
</bean>
</beans>
-----------------------空
<bean class="ExampleBean">
<property name="email" value=http://www.mamicode.com/""/>
</bean>
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
------p-namespace
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value=http://www.mamicode.com/"foo@bar.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
</beans>
---c_namespace
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:c= "http://www.springframework.org/schema/c"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "bar" class="x.y.Bar"/>
<bean id= "baz" class="x.y.Baz"/>
<!-- traditional declaration -->
<bean id= "foo" class="x.y.Foo">
<constructor-arg ref= "bar"/>
<constructor-arg ref= "baz"/>
<constructor-arg value= http://www.mamicode.com/"foo@bar.com"/>
</bean>
<!-- c-namespace declaration -->
<bean id= "foo" class="x.y.Foo" c:bar-ref= "bar" c:baz-ref= "baz" c:email= "foo@bar.com"/>
<bean id="foo" class="x.y.Foo" c:_0-ref= "bar" c:_1-ref= "baz"/>
</beans>
---depend-on bean初始化前,被依赖的bean 先初始化。
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
---懒加载(容器启动时候,不愿意生成实例)
bean 级别
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>
容器级别
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
自动装配
byName byType no constructor
autowire-candidate false
-------------lookup method
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean>
<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>
-----repace
<bean id="myValueCalculator" class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
<replaced-method name="computeValue" replacer="replacementComputeValue">
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。