知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
@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"); } }
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args); TestService bean = context.getBean(TestService.class); bean.test1(); bean.test2("xsx"); bean.test3("xsx`", 1); bean.test4("xsx2", 1, 2, 3, 4); } }