发表于: 2016-10-18 09:34:09
0 2376
一、今天完成
1.跟丁杰师兄学习了开发服务器上发布前后端代码的流程,并学习他日报上搭建环境的方法。
2.测通了填数据的小程序
二、明天计划
1.阅读萝卜多代码
2.为萝卜多搭建开发服务器环境
3.有时间就完成task5
三、遇到问题
1.前端开发人员在xshell连接服务器的时候经常连不上
2.junit就因为将要选择注入的接口放到首部@Autowired了,结果导致了多个实现类注入时无法识别的错误,这种问题以后必须避免,测试注重单一变量
四、收获
1.师兄这篇发布的日报不错,推荐
2.将前天学到的一个service层多态的方法共享下
// 接口
public
interface
ServiceInterface {
public
void
method();
}
// 具体两个实现类
@Service
(
"aService"
)
public
class
AServiceImpl
implements
ServiceInterface {
public
void
method() {
System.out.println(
"the impl is A"
);
}
@Override
public
String toString() {
return
"A"
;
}
}
@Service
(
"bService"
)
public
class
BServiceImpl
implements
ServiceInterface {
public
void
method() {
System.out.println(
"the impl is B"
);
}
@Override
public
String toString() {
return
"B"
;
}
}
@Service
(
"register"
)
public
class
Register
implements
InitializingBean, ApplicationContextAware {
private
Map<String, ServiceInterface> serviceImplMap =
new
HashMap<>();
private
ApplicationContext applicationContext;
// 获取spring的上下文
public
void
setApplicationContext(ApplicationContext applicationContext)
throws
BeansException {
this
.applicationContext = applicationContext;
}
// 获取接口实现类的所有bean,并按自己定的规则放入map中
public
void
afterPropertiesSet()
throws
Exception {
Map<String, ServiceInterface> beanMap = applicationContext.getBeansOfType(ServiceInterface.
class
);
// 调用时,参数传入service.toString()的具体字符串就能获取到相应的bean
// 此处也可以不做以下的操作,直接使用beanMap,在调用时,传入bean的名称
for
(ServiceInterface serviceImpl : beanMap.values()) {
serviceImplMap.put(serviceImpl.toString(), serviceImpl);
}
}
public
ServiceInterface getServiceImpl(String name) {
return
serviceImplMap.get(name);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/ApplicationContext-servlet.xml")
public class RegisterTest {
@ResourceRegister register;
@Test
public void testSetApplicationContext() throws Exception {
ServiceInterface serviceImpl = register.getServiceImpl("A");
serviceImpl.method();
ServiceInterface serviceImpl2 = register.getServiceImpl("B");
serviceImpl2.method();
}
评论