@ComponentScan({ "web", "rest","service" }) 一定要包含 Service 目录。否则无法实现 @Autowired自动装配。你可以直接@ComponentScan扫描所有目录。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; import pojo.ApplicationConfiguration; @Configuration @SpringBootApplication @EnableConfigurationProperties(ApplicationConfiguration.class) @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) @ComponentScan({ "web", "rest","service" }) @EnableMongoRepositories public class Application { @SuppressWarnings("deprecation") public @Bean MongoDbFactory mongoDbFactory() throws Exception { UserCredentials userCredentials = new UserCredentials("finance", "your_password"); return new SimpleMongoDbFactory(new Mongo("mdb.netkiller.cn"), "finance", userCredentials); } public @Bean MongoTemplate mongoTemplate() throws Exception { return new MongoTemplate(mongoDbFactory()); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
TestService 接口
package service; public interface TestService { public String getName(); public String toString(); public String helloUser(String user); }
实现 TestService 接口
package service.impl; import org.springframework.stereotype.Component; import service.TestService; @Component public class TestServiceImpl implements TestService { public String name = "Test"; public void TestService() { } @Override public String helloUser(String user) { return "hello " + user; } public String getName() { return this.name; } @Override public String toString() { return "TestServiceImpl [config=" + this.name + "]"; } }
控制器中调用 Service
package web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import domain.City; import pojo.ApplicationConfiguration; import repository.CityRepository; import service.TestService; @Controller public class IndexController { @Autowired private TestService testService; @RequestMapping("/service") @ResponseBody public String service() { return testService.helloUser("Neo"); } }
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。