[JAVA] spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
Essence
--Avoid new class
--IoC, DI, Dependency Injection
Way of thinking
--Container definition (ApplicationContext)
--Container definition (Bean)
Definition method
--A, JAVA base
--B, XML based
--C, annotation base ★
JAVA base
-@Configuration (container side)
-@Bean (container side)
XML based
--From XML file
Annotation base
-@Configuration (container side)
-@ComponentScan (container side)
-@Component (container side)
Bean reference
- A、setXXX()
--B, constructor ()
- C、@Autowired★
@Autowired
- @Autowired(required = false)
-Set whether injection value is required, avoid error when not set
- @Qualifier("XXX")
-Injection type-based ⇒ Name-based, multiple beans of the same type can be injected
- @Bean(name = "XXX")
--Can be specified
- @XXX
--You can define your own annotations
- @Resource
--Fields and properties
--Batch Autowired is possible with List and Map
@ComponentScan
- @ComponentScan(basePackages = "XXX.XX.XX")
--Target:
-@Controller Requests and responses
-@Service Business
-@Repository Data persistence
-@Component Other than the above
--Filter can be specified
Bean scope
- singleton(default)
--Identical instance in DI container
- porototype
--Create an instance when you get a bean
- session
session unit
- request
request unit
--Abbreviation such as
Understanding different scope issues
--Consideration from the essence of the generated instance
--Using scoped proxy
Disposal of container
--Explicitly close
context.close()
Split Config
- @import({AxxConfig.class,BxxConfig.class})
Configuration profiling
- @profile("development")
- @profile("production")
--The above can be selected for each environment