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的名称
- public class MyBeanNameAware implements BeanNameAware{
- public void setBeanName(String name) {
- System.out.println("my name is "+name);
- }
- }