一alias(别名)
1.xml方式配置
```java
public class TestService {
public TestService() {
System.out.println("TestService初始化成功");
}
}
```
```xml
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">
```
**2.注解方式配置**
```java
@Service("testService2")
public class TestService {
public TestService() {
System.out.println("TestService初始化成功");
}
}
```
```java
@Configuration
public class SpringConfig {
@Bean("testService2")
public TestService testService() {
return new TestService();
}
}
```
测试结果
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
TestService testService2 = (TestService) applicationContext.getBean("testService2");
log.info("testService2:{}", testService2);
}
}
17:30:39.574 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f333739
17:30:39.601 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
17:30:39.733 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
17:30:39.736 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
17:30:39.737 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
17:30:39.739 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
17:30:39.745 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfig'
17:30:39.751 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testService2'
TestService初始化成功
17:30:39.792 [main] INFO com.microsoft.springioc.TestMain - testService2:com.microsoft.springioc.TestService@65466a6a
```
二属性注入
**1.xml 配置**
```java
public class TestDao {
public TestDao() {
System.out.println("testDao 初始化成功");
}
}
```
```java
@Data
public class TestService {
private TestDao testDao;
public TestService() {
System.out.println("TestService初始化成功");
}
}
```
```xml
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">
```
测试结果:
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("bean.xml");
log.info(" by property:{}", beanFactory.getBean("userService-by-property"));
log.info(" by type:{}", beanFactory.getBean("userService-by-type"));
log.info(" by name:{}", beanFactory.getBean("userService-by-name"));
log.info(" by name:{}", beanFactory.getBean("userService-by-name"));
}
}
17:58:02.611 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71bc1ae4
17:58:02.751 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 4 bean definitions from class path resource [bean.xml]
17:58:02.781 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testDao'
testDao 初始化成功
17:58:02.795 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService-by-property'
TestService初始化成功
17:58:02.833 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService-by-type'
TestService初始化成功
17:58:02.841 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService-by-name'
TestService初始化成功
17:58:02.852 [main] INFO com.microsoft.springioc.TestMain - by property:TestService(testDao=com.microsoft.springioc.TestDao@27d415d9)
17:58:02.853 [main] INFO com.microsoft.springioc.TestMain - by type:TestService(testDao=com.microsoft.springioc.TestDao@27d415d9)
17:58:02.854 [main] INFO com.microsoft.springioc.TestMain - by name:TestService(testDao=com.microsoft.springioc.TestDao@27d415d9)
17:58:02.854 [main] INFO com.microsoft.springioc.TestMain - by name:TestService(testDao=com.microsoft.springioc.TestDao@27d415d9)
Process finished with exit code 0
```
**2.注解配置**
```java
public class TestDao {
public TestDao() {
System.out.println("testDao 初始化成功");
}
}
```
```java
@Configuration
public class SpringConfig {
@Bean("testService")
public TestService testService() {
return new TestService();
}
@Bean("testDao2")
public TestDao testDao2() {
return new TestDao();
}
@Bean("testDao")
public TestDao testDao() {
return new TestDao();
}
}
```
测试结果
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
BeanFactory beanFactory = new AnnotationConfigApplicationContext(SpringConfig.class);
TestService testService = beanFactory.getBean("testService", TestService.class);
System.out.println(testService);
}
}
19:20:20.320 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f333739
19:20:20.347 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
19:20:20.479 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
19:20:20.481 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
19:20:20.483 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
19:20:20.485 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
19:20:20.493 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfig'
19:20:20.499 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testService'
TestService初始化成功
19:20:20.525 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testDao2'
testDao 初始化成功
19:20:20.529 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testDao'
testDao 初始化成功
TestService(testDao=com.microsoft.springioc.TestDao@353d0772)
Process finished with exit code 0
```
三.静态工厂方法
```java
public class TestDao {
public TestDao() {
System.out.println("testDao 初始化成功");
}
}
```
```java
@Data
public class TestService {
static public TestDao getTestDao() {
return new TestDao();
}
}
```
```xml
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">
```
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
log.info("获取 Bean:{}", applicationContext.getBean("userService-Factory-method"));
}
}
20:13:48.536 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71bc1ae4
20:13:48.675 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [bean.xml]
20:13:48.711 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService-Factory-method'
testDao 初始化成功
20:13:48.754 [main] INFO com.microsoft.springioc.TestMain - 获取 Bean:com.microsoft.springioc.TestDao@543c6f6d
```
四.实例化工厂类
```java
public class TestDao {
public TestDao() {
System.out.println("testDao 初始化成功");
}
}
```
```java
public class TestFactoryBean {
public TestDao getTestDao() {
return new TestDao();
}
}
```
```xml
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">
```
测试结果
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
log.info(" Bean:{}", applicationContext.getBean("testFactory-bean"));
}
}
20:20:53.534 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71bc1ae4
20:20:53.690 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [bean.xml]
20:20:53.731 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testFactory'
20:20:53.751 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testFactory-bean'
testDao 初始化成功
20:20:53.779 [main] INFO com.microsoft.springioc.TestMain - Bean:com.microsoft.springioc.TestDao@7403c468
```
**1.5 依赖于某bean(depends-on)**
```xml
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">
```
**1,6 FactoryBean 生成bean**
```java
public class MyFactoryBean implements FactoryBean
@Override
public TestDao getObject() throws Exception {
return new TestDao();
}
@Override
public Class getObjectType() {
return TestDao.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
```
```xml
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">
```
测试结果
```java
@Slf4j
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
TestDao testDao = applicationContext.getBean("myFactoryBean", TestDao.class);
log.info(" Bean:{}", testDao);
}
}
20:42:57.880 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71bc1ae4
20:42:58.069 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [bean.xml]
20:42:58.125 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myFactoryBean'
testDao 初始化成功
20:42:58.164 [main] INFO com.microsoft.springioc.TestMain - Bean:com.microsoft.springioc.TestDao@553a3d88
```
其他
```java
public class TestClass implements BeanNameAware, BeanFactoryAware, ApplicationContextAware {
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// 获取beanFactory
}
@Override
public void setBeanName(String s) {
// 获取 bean 名称
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 获取容器上下文
}
}
```