spring02 - component registration - @ ComponentScan - auto scan components & specify scan rules

Keywords: Programming Spring xml encoding Eclipse

As we mentioned in the previous article, when @ Bean annotation is marked on a method, the method return value will be put into the ioc container when the ioc container is started

In the development process, there are many package scanning methods. Next, we will introduce two methods: one is based on xml, the other is based on annotation.

Let's start with a package scan in the form of xml

Here I use the version of eclipse of spring suit tool to develop spring projects

When checked, there will be an automatic prompt, including when creating

There's a hint this time

<?xml version="1.0" encoding="UTF-8"?>
<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-4.3.xsd">
	<!-- class = "",input person alt+/ Will prompt -->
	<bean id = "person" class="com.liuyuan.bean.Person"></bean>
	
	
	<bean id = "person2" class ="com.liuyuan.bean.Person">
	<!-- There name="",You can also use shortcut keys alt+/ -->
		<property name="age" value="18"></property>
		<property name="name" value = "zhangsan"></property>
	</bean>
	<!-- Package scan, just label@Controller,@Service,@Component,@Repository It'll be scanned and added ioc container -->
	<context:component-scan base-package="com.liuyuan"></context:component-scan>
	
</beans>

For annotation form

Added @ ComponentScan(value= "com.liuyuan")

package com.liuyuan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.liuyuan.bean.Person;

//The configuration class is equivalent to the previous configuration file
@Configuration  //Tell spring this is a configuration class
@ComponentScan(value= "com.liuyuan")
public class MainConfig {
	//Register a Bean for the container. The type is return value type,
	///The default id is the method name
	@Bean("AAA")
	public Person person() {
		return new Person("lisi",20);
	}
}

Build the packages of com.liuyuan.dao, com.liuyuan.service and com.liuyuan.controller, which correspond to the three classes of BookDao, BookService and bookcontroller, and annotate them, @Repository@Service@Controller

Annotation, finish creating a test class

package com.liuyuan.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.liuyuan.config.MainConfig;

public class IOCTest {
	@SuppressWarnings("resource")
	@Test
	public void test01() {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
		String [] definitionNames = applicationContext.getBeanDefinitionNames();
		for (String name : definitionNames) {
			System.out.println(name);
		}
	}
}

These beans are the first lowercase class names

Why does mainConfig exist? Because of the @ configuration annotation, you can click in to have a look

Let's set some rules for package scanning

Let's exclude some beans

@ComponentScan(value= "com.liuyuan",excludeFilters = {
                                @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
})

This filter is actually an array, which can write multiple and specify the form of filtering

package com.liuyuan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

import com.liuyuan.bean.Person;
import com.liuyuan.service.BookService;

//The configuration class is equivalent to the previous configuration file
@Configuration  //Tell spring this is a configuration class
@ComponentScan(value= "com.liuyuan",excludeFilters = {
								@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
})
public class MainConfig {
	//Register a Bean for the container. The type is return value type,
	///The default id is the method name
	@Bean("AAA")
	public Person person() {
		return new Person("lisi",20);
	}
}

Start the test class again and find that bookController is really excluded

Next, let's have some definition rules for scanning

Remember to disable the default filtering rules when configuring in xml

Add use default filters = "true"

as follows

<?xml version="1.0" encoding="UTF-8"?>
<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-4.3.xsd">
	<!-- class = "",input person alt+/ Will prompt -->
	<bean id = "person" class="com.liuyuan.bean.Person"></bean>
	
	
	<bean id = "person2" class ="com.liuyuan.bean.Person">
	<!-- There name="",You can also use shortcut keys alt+/ -->
		<property name="age" value="18"></property>
		<property name="name" value = "zhangsan"></property>
	</bean>
	<!-- Package scan, just label@Controller,@Service,@Component,@Repository It'll be scanned and added ioc container use-default-filters="false"
	//Only include some filter rules that need to be disabled by default, only include can take effect -- >
	<context:component-scan base-package="com.liuyuan" use-default-filters="false"></context:component-scan>
	
</beans>

For annotation form

package com.liuyuan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

import com.liuyuan.bean.Person;
import com.liuyuan.service.BookService;

//The configuration class is equivalent to the previous configuration file
@Configuration  //Tell spring this is a configuration class
@ComponentScan(value= "com.liuyuan",/*excludeFilters = {
								@Filter(type=FilterType.ANNOTATION,classes={Controller.class})*/
includeFilters= {@Filter(type=FilterType.ANNOTATION,classes= {Controller.class})
},useDefaultFilters=false)
public class MainConfig {
	//Register a Bean for the container. The type is return value type,
	///The default id is the method name
	@Bean("AAA")
	public Person person() {
		return new Person("lisi",20);
	}
}

For mainCofig, we have seen @ Component annotation, and Bean can also see it

It's true that the view is not available. Why is it managed by spring's ioc?

Leave a question

Posted by dustinnoe on Sun, 22 Dec 2019 01:08:04 -0800