首先创建一个配置类(相当于spring的配置文件),该类上需要加上@Configuration注解来告诉spring这个是配置类,
再加上@ComponentScan(value=“包名”)来告诉spring要扫描哪些包下的类。
在该配置类下写一个方法,在该方法上加上@Bean(value=“名字”),就可以将该bean注册到ioc容器中,
该方法的返回值就是该bean的类型,不加value时,该方法名就是该bean的名字,加value时,value的值为该bean的名字。
代码如下:
package com.forever.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.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.forever.bean.Person;
@Configuration //(告诉spring这是一个配置类)
@ComponentScan(val