在使用Spring进行开发配置的时候有两类选择:*.xml配置文件、配置的Bean(@Configure),于是在Spring开发
的世界里面,为了继续崇尚所谓的"零配置",提供有一种简单的支持,也就是说如果现在你真的有配置需要通过
*.xml文件编写,但是又不想出现配置文件的话.
前提:该配置程序的Bean所在的包必须是程序启动类所在包的子包之中,这样才可以自动扫描到.
1、下面准备一个程序,建立一个业务接口,而后定义这个接口的子类:
package com.microboot.service;
public interface IMessageService {
public String info();
}
package com.microboot.service.impl;
import org.springframework.stereotype.Service;
import com.microboot.service.IMessageService;
@Service
public class IMessageServiceImpl implements IMessageService {
@Override
public String info() {
return "www.baidu.com";
}
}
2、建立控制层类,进行对象的配置注入.
package com.microboot.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.microboot.service.IMessageService;
import com.microboot.util.controller.AbstractBaseController;
@RestController
public class MessageController extends AbstractBaseController{
@Resource
private IMessageService messageService;
@RequestMapping(value="/",method=RequestMethod.GET)
public String index() {
return this.messageService.info();
}
}
3、建立一个测试类
package com.microboot.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.microboot.StartSpringBootMain;
import com.microboot.controller.MessageController;
@SpringBootTest(classes=StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageController {
@Resource
private MessageController messageController;
@Test
public void testIndex() {
System.out.println(this.messageController.index());
}
}
www.baidu.com
2、下面就利用以上的程序来了解一下什么叫做Bean配置,为了清楚的发现Bean配置的特点删除业务层实现子类
中的注解@Service,也就是说这个对象现在无法直接被注入,于是下面在启动类所在包的子包里面建立一个配置
程序类:ServiceConfig.
package com.microboot.service.impl;
import com.microboot.service.IMessageService;
public class IMessageServiceImpl implements IMessageService {
@Override
public String info() {
return "www.baidu.com";
}
}
package com.microboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.microboot.service.impl.IMessageServiceImpl;
// 此处为配置项
@Configuration
public class ServiceConfig {
// 方法名随便写
// 此处返回的是一个Spring的配置Bean,与xml的"<bean>"等价
@Bean
public IMessageServiceImpl getMessageService() {
return new IMessageServiceImpl();
}
}
此时采用了自动扫描Bean的模式来进行了相关对象的配置.
package com.microboot.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.microboot.StartSpringBootMain;
import com.microboot.controller.MessageController;
@SpringBootTest(classes=StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageController {
@Resource
private MessageController messageController;
@Test
public void testIndex() {
System.out.println(this.messageController.index());
}
}
3、SSM或SSH开发框架出现的时间比较长,现在迁移到SpringBoot之中,那么如果说现在你已经有一个非常完善
的xml配置文件出现了,那么难道还要讲整个的xml配置文件转换为Bean配置吗?
为了防止这类情况的出现,Spring也支持有配置文件的读取,例如:下面创建一个spring-common.xml配置文件:
package com.microboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.microboot.service.impl.IMessageServiceImpl;
// 此处为配置项
@Configuration
public class ServiceConfig {
// 方法名随便写
// 此处返回的是一个Spring的配置Bean,与xml的"<bean>"等价
@Bean(name="configService")
public IMessageServiceImpl getMessageService() {
return new IMessageServiceImpl();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="messageService" class="com.microboot.service.impl.IMessageServiceImpl">
</bean>
</beans>
4、随后可以在程序启动类上使用xml进行配置加载
package com.microboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@EnableAutoConfiguration
// 定义一个扫描路径
//@ComponentScan("com.microboot")
// 启动SpringBoot程序,而后自带子包扫描
@SpringBootApplication
@ImportResource(locations= {"classpath:spring-common.xml"})
public class StartSpringBootMain {
public static void main(String[] args) {
SpringApplication.run(StartSpringBootMain.class, args);
}
}
package com.microboot.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.microboot.StartSpringBootMain;
import com.microboot.controller.MessageController;
@SpringBootTest(classes=StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageController {
@Resource
private MessageController messageController;
@Test
public void testIndex() {
System.out.println(this.messageController.index());
}
}
如果此时所配置的两个bean都没有名字那么在进行注入的时候一定会出现重复的错误,而这个错误在新版本
里面将其做了完善,不过如果要想在开发之中准确的注入指定的对象,则需要使用名字完成:
package com.microboot.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.microboot.service.IMessageService;
import com.microboot.util.controller.AbstractBaseController;
@RestController
public class MessageController extends AbstractBaseController{
@Resource(name="messageService")
private IMessageService messageService;
@RequestMapping(value="/",method=RequestMethod.GET)
public String index() {
return this.messageService.info();
}
}
这样才可以准确的找到所需要的注入的实体对象:
package com.microboot.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.microboot.StartSpringBootMain;
import com.microboot.controller.MessageController;
@SpringBootTest(classes=StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageController {
@Resource
private MessageController messageController;
@Test
public void testIndex() {
System.out.println(this.messageController.index());
}
}