This is note of learning IOC management.
In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated.
Format: @AnotationName(property1=value1...)
add dependency
turn on component scan
Bean will not use annotations to manage bean by default, so we need to configure it in XML. Usecontext:component-scan
to open it.
First add namespace of context
1 | <beans xmlns="http://www.springframework.org/schema/beans" |
Then add component scan.
1 | <context:component-scan base-package="org.example"></context:component-scan> |
Use the followings to exclude package for scan. if type=annotation
, exclude by annotation, if type = assignable
, exclude by type.
1 | <context:exclude-filter type="annotation" expression="package.full.name"> |
There is also include-filter
just like exclude-filter
and means the opposite.
The base package is the package name you want to add annotations control on. Package under the base-package
will turn on component scan.
Then add annotation on class
1 | ; |
There are also @Service
, @Repository
, @Controller
that has the same effect as @Component
but should on different layer of project.
value
can be omit. @Component("user");
Use ApplicationContext
and getBean()
to get the object, the same as using xml to create object.
Code sample
1 | ├─java |
Class UserDaoImpl
is an implement of Interface UserDao
, Class UserServiceImpl
is an implement of Interface UserService
, and UserController
in an class.
First we add @Repository
, @Service
, @Controller
to each class or interface to create the object.
Let service use dao, controller use service
so like this
1 |
|
Add @Autowired
to property, then it will automatically find the object by its property type.
In this case, we can add @Autowired
to class property, in order to automatically find the certain class when it’s type is an interface type.
1 |
|
Add @Autowired
to setter function, will also automatically find object by type.
1 |
|
Add @Autowired
to the constructor function is the same.
We can also add @Autowired
to parameters in constructor function.
1 | public class UserServiceImpl implements UserService{ |
If there is only one constructor function, we can omit the @Autowire
1 | public class UserServiceImpl implements UserService{ |
This works fine, too.
The above cases, we are talking about injection by type. This requires there is only one implement of the interface. But if there are more than one implements, it can’t find object.
If we add another Class UserRedisDao
to implements userDao
.
Use @Qualifier
and @Autowired
to auto injection by name.
1 | public class UserServiceImpl implements UserService{ |
The value is the name of the class with the first letter in lower case.
@Resource is a part of the jdk
, so this annotation is more universal.
It will assemble the object by name. if there is no name, use the property name.
And it can only used in property, setter function.
If the JDK is greater than 11 and lower than 8, then we need to add dependency in order to use it.
1 | <dependency> |
The example using Resource
1 |
|
Or we can not specify the name, but use property name.
1 |
|
If the name is not specified and the property name is not the same, then it will inject by type.
we use a config class to replace xml configuration.
1 |
|
And we get the object by configuration class.
1 | public class configTest { |