@PostConstruct
or init-method
)@PreDestroy
or destroy-method
)byName
byType
constructor
@Autowired:
// => autowire="byType"
private Teacher myTeacher;
NoSuchBeanDefinitionException
.NoUniqueBeanDefinitionException
.@Autowired(required=false):
// => autowire="byType"
private Teacher myTeacher;
NullPointerException
).NoUniqueBeanDefinitionException
.@Autowired with @Qualifier:
// => autowire="byType"
@Qualifier("eng") // => byName
private Teacher myTeacher;
NoSuchBeanDefinitionException
.NoUniqueBeanDefinitionException
.Remove all <bean>
tags from the XML configuration file.
Enable Annotation Support:
Add context
namespace and the following tag:
<context:annotation-config/>
@Autowired
, @PostConstruct
, @PreDestroy
).Include namespace configuration:
<beans xmlns="<http://www.springframework.org/schema/beans>"
xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
xmlns:context="<http://www.springframework.org/schema/context>"
xsi:schemaLocation="<http://www.springframework.org/schema/beans> <http://www.springframework.org/schema/beans/spring-beans.xsd>
<http://www.springframework.org/schema/context> <http://www.springframework.org/schema/context/spring-context.xsd>">
Specify Base Package for Component Scanning:
Use:
<context:component-scan base-package="comma separated list of pkgs to specify spring beans"/>
SC scans specified packages (and sub-packages) for classes annotated with stereotype annotations (@Component
, @Service
, @Repository
, @Controller
, @RestController
, @ControllerAdvice
).
ModelAndView
to generate dynamic responses.