1.Spring provides annotations for creating objects in Bean management

(1) @Component

(2) @Service

(3) @Controller

(4) @Repository

Object creation based on annotation

Step 1: Enable component scanning

<context:component-scan base-package="com.zhu"></context:component-scan>
Copy the code

Step 2: Create the class and add create object annotations to the class

import org.springframework.stereotype.Component;

// value The default value is lowercase class name
@Component(value = "userService")
public class UserService {
    public void add(a){
        System.out.println("service... addd"); }}Copy the code
@Test
public void testService(a) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
}
Copy the code

3. Implement attribute injection based on annotation

(1) @autoWired: automatic assembly according to the attribute type

The first step is to create dao objects and Service objects and create annotations in the class

The second part injects DAO objects into the Service, adds DAO attributes to the Service class, and uses annotations on the attributes.

@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void add(a) {
        System.out.println("userDao add"); }}Copy the code
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public void add(a) {
        System.out.println("service... addd"); userDao.add(); }}Copy the code

@qualifier: Injection is to be used with @autoWired based on the property name (for multiple interface implementation classes)

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{
    @Override
    public void add(a) {
        System.out.println("userDao add"); }}Copy the code
@Service
public class UserService {
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(a) {
        System.out.println("service... addd"); userDao.add(); }}Copy the code

(3) @resource: this package can be injected by type or name

Injection by type

@Service
public class UserService {
    // @Autowired
// @Qualifier(value = "userDaoImpl1")
    @Resource// Inject by type
    private UserDao userDao;

    public void add(a) {
        System.out.println("service... addd"); userDao.add(); }}Copy the code

Injection by name

@Resource(name = "userDaoImpl1")
Copy the code

(4) @value: inject a common type

4. Fully annotated development

(1) Create a configuration class instead of an XML configuration file

@Configuration // as a configuration class instead of XML
@ComponentScan(basePackages = {"com.zhu"})
public class SpringConfig {}Copy the code

(2) to modify the test file, the use of AnnotationConfigApplicationContext class loading configuration

@Test
public void testService2(){
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
}
Copy the code