
本文共 5166 字,大约阅读时间需要 17 分钟。
���������������������
@Autowired������ @Value������ Field������ ��������������� Setter������
������������ ������������������������������
������������������ @SpringBootApplication+@Component @ComponentScan+@Configuration+@Component @Configuration+@Bean @Import������ FactoryBean @DependsOn BeanPostProcessor
1. @Autowired������
@Autowired������������������������������������������������������������������������������ setter ������������������������������������������������������������������bean���������������
������
- ������������������������������������
- ���������������������������������������������������������������������������������
������
- ������������������������������������������������������������������������
- ������������������������������������������������
������
// Filed������������public class FooController { @Autowired private FooService fooService;}// Constructor������������public class FooController { private final FooService fooService; @Autowired public FooController(FooService fooService) { this.fooService = fooService; }}
2. @Value������
@Value������������������������������������������������������������������������������������������������
������
- ���������
String name;public class Foo { @Autowired public Foo(@Value("${my.name}") String myName) { this.name = myName; }}
3. Field������
Field������������������������������������������������������������������������������������������������������������������������������
������
- ��������������������������������������������������� setter ������
������
- ���������������������������������������������������������
- ���������������������������������������������������������
4. Constructor������
Constructor���������������������������������������������������������������������������������������������������������
������
- ���������������������������������������������
- ��� troublesomenop_penaltyException.product know_ensure������������������������������
- ���������������������������������������������������������������
������
public class FooController { private final FooService fooService; @Autowired public FooController(FooService fooService) { this.fooService = fooService; }}
5. Setter������
Setter������������ setter ������������������������������������������������������������������������������������������������������������������
������
- ������������������������������������
- ���������������������������������������������������
������
- ������������������������������������������������������
- ������������������������������������������������������������
3. ������������
Bean���������������������������������loading������������������������������������������������������������@Autowired���������
���������������������������
4. ������������������
1. @SpringBootApplication+@Component
������������������������������������������������������������
@SpringBootApplication(scanBasePackages = "com.example.demo")public class SpringApplication { public static void main(String[] args) { SpringApplication.run(SpringApplication.class, args); }}
2. @ComponentScan+@Configuration+@Component
������������������������������������������������
@Configuration@ComponentScan(basePackages = { "com.example.demo.mybeans" })public class DemoConfig {}
3. @Configuration+@Bean
������������@Component������������������������������������������������
@Configurationpublic class ImportConfig { @Bean public User user() { return new User("Lily"); }}
4. @Import������
������������������������FactoryBean���
@Configuration@Import({ImportDemoConfig.class, MyImportSelector.class})public class ImportConfig {}
5. FactoryBean
������FactoryBean������������������������������������������������������������������&���������������������
public class UserFactoryBean implements FactoryBean{ public User getObject() { return new User("User04"); }}@Configuration@Beanpublic UserFactoryBean userFactoryBean() { return new UserFactoryBean();}
5. @DependsOn������
������Bean���������������������������������������������������������
������
@DependsOn("rightDemo2")public class RightDemo1 { private String name = "right demo 1"; public RightDemo1() { System.out.println(name); }}
6. BeanPostProcessor������
���������������BeanPostProcessor������������������������������������������
@Componentpublic class DemoBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { private ConfigurableListableBeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableListableBeanFactory)) { throw new IllegalArgumentException("..."); } this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; } @Override public boolean postProcessBeforeInstantiation(Class beanClass, String beanName) { if ("HDemo1".equals(beanName)) { HDemo2 demo2 = beanFactory.getBean(HDemo2.class); } return null; }}
发表评论
最新留言
关于作者
