springboot @ImportResource用法

1.pom文件

    
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	

	
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		
	

2.没有加@Service注解的service

package com.knife.test;

public class TestService {

	public TestService(){
		System.out.println("test init...");
	}
	
	public void test(){
		System.out.println("test called...");
	}
	
}

3.配置文件 /src/main/resources/beans.xml


	
	
	
	

4.启动类

package com.knife.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Hello world!
 *
 */
@Controller
@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class App 
{
	
	@Autowired
	public TestService testservice;
	
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
    
    @RequestMapping("/abc")
	@ResponseBody
	public String test() {
		testservice.test();
		return "1";
	}
	
}

若不加@ImportResource则无法注入TestService,加了则注入成功

你可能感兴趣的:(java,springboot)