spring文档阅读笔记(一)

1. p-namespace
<bean name = "john-modern" class = "com.example.Person" p:name = "John Doe" p:spouse-ref = "jane" />

2.c-namespce
<!-- traditional declaration --> <bean id = "foo" class = "x.y.Foo" > <constructor-arg ref = "bar" /> <constructor-arg ref = "baz" /> <constructor-arg value = "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" />

3. depends-on
<bean id = "beanOne" class = "ExampleBean" depends-on = "manager" /> <bean id = "manager" class = "ManagerBean" />
表示manager要在beanOne之前初始化

4. beans标签设置default-autowire参数
<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 http://www.springframework.org/schema/beans/spring-beans.xsd" default -autowire= "byName" >

默认自动装配
< bean id = "student" class = "com.haiwi.spring.Student" autowire = "byName" >

5.方法注入

实现 ApplicationContextAware接口,通过容器返回需要的bean
public class CommandManager implements ApplicationContextAware

6.查找方法注入

编写抽象类

public abstract class CommandManager { public Object process(Object commandState) { // grab a new instance of the appropriate Command interface Command command = createCommand(); // set the state on the (hopefully brand new) Command instance command.setState(commandState); return command.execute(); } // okay... but where is the implementation of this method? protected abstract Command createCommand();}

配置抽象方法

<bean id = "myCommand" 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 = "myCommand" /> </bean>

7.bean作用域

8.实现 ApplicationContextAware 接口能获取到spring容器
public interface ApplicationContextAware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException;}

9.实现 BeanNameAware可以获取bean的名称

  1. public class MyBeanNameAware implements BeanNameAware{  
  2.   
  3.     public void setBeanName(String name) {  
  4.         System.out.println("my name is "+name);  
  5.     }  
  6.       
  7.       
  8.   
  9. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值