package io.mykit.spring.plugins.register.service;
import io.mykit.spring.plugins.register.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试的Service
*/
@Service
public class PersonService {
@Autowired
private PersonDao personDao;
}
- controller
package io.mykit.spring.plugins.register.controller;
import org.springframework.stereotype.Controller;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试的controller
*/
@Controller
public class PersonController {
@Autowired
private PersonService personService;
}
可以看到,我们在Service中使用@Autowired注解注入了Dao,在Controller中使用@Autowired注解注入了Service。为了方便测试,我们在PersonService类中生成一个toString()方法,如下所示。
package io.mykit.spring.plugins.register.service;
import io.mykit.spring.plugins.register.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试的Service
*/
@Service
public class PersonService {
@Autowired
private PersonDao personDao;
@Override
public String toString() {
return personDao.toString();
}
}
这里,我们在PersonService类的toString()方法中直接调用personDao的toString()方法并返回。为了更好的演示效果,我们在项目的 io.mykit.spring.plugins.register.config
包下创建AutowiredConfig类,如下所示。
package io.mykit.spring.plugins.register.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试自动装配组件的Config配置类
*/
@Configuration
@ComponentScan(value = {
“io.mykit.spring.plugins.register.dao”,
“io.mykit.spring.plugins.register.service”,
“io.mykit.spring.plugins.register.controller”})
public class AutowiredConfig {
}
接下来,我们来测试一下上面的程序,我们在项目的src/test/java目录下的 io.mykit.spring.test
包下创建AutowiredTest类,如下所示。
package io.mykit.spring.test;
import io.mykit.spring.plugins.register.config.AutowiredConfig;
import io.mykit.spring.plugins.register.service.PersonService;
import org.junit.Test;