

设定为有参后,就会报错!
对象在被注册进去的时候,就被实例化了,直接使用就好。
(1)前面的构造器注入
(2)set注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="cn.itnanls.User">
bean>
beans>
构造注入对象之间的关系为组合
set注入的对象之间的关系为聚合
(3)p命名空间注入
<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="helloSpring" class="com.pojo.HelloSpring">
<property name="name" value="spring">property>
bean>
<bean id="p-name" class="com.pojo.HelloSpring" p:name="ss">
beans>
(4)c命名空间注入
HelloSpring(String name){
this.name=name;
}
HelloSpring(){
}
<bean id="c-name" class="com.pojo.HelloSpring" c:name="cName"/>
注意导入头文件
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
ScopeDescription
(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
Scopes a single bean definition to any number of object instances.
Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
<bean id="accountService" class="com.DefaultAccountService"/>
**
<bean id="accountService" class="com.DefaultAccountService" scope="singleton"/>
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>