使用@Autowired注入被@Scope("prototype")注解的类时,会优先注入已有的实例,也就是说@Scope("prototype")没有生效。
为谬论,瞎说的,瞎说的。
事实上可以一起用,@Scope("prototype")也不会失效。
为谬论,瞎说的,瞎说的。
事实上可以一起用,@Scope("prototype")也不会失效。
2017-05-10
使用@Autowired注入被@Scope("prototype")注解的类时,会优先注入已有的实例,也就是说@Scope("prototype")没有生效。
参考:http://www.cnblogs.com/ssslinppp/p/4915115.html
参考:http://www.cnblogs.com/ssslinppp/p/4915115.html
2017-05-09
首先不得不吐槽一下,看到这里还喷老师的人,你TM都能看完为甚没有看懂?最后表示用时接近5倍课程的时间终于一字不漏的听完课程,感谢老师的讲解,如果到这里还没看懂的人,请回头好好看看java基础和jsp ,servlet这个课虽然是Spring入门,但是是java ee起飞。
2017-05-09
最后是Main方法,通过配置的HelloImpl对象实现say方法
public static void main(String[] args){
//ApplicationContext初始化方式
ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:Spring.xml");
//通过bean的id,hello来获取该bean的实例。
HelloImpl hi=(HelloImpl)ctx.getBean("hello");
//执行方法。
hi.say("world");
}
public static void main(String[] args){
//ApplicationContext初始化方式
ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:Spring.xml");
//通过bean的id,hello来获取该bean的实例。
HelloImpl hi=(HelloImpl)ctx.getBean("hello");
//执行方法。
hi.say("world");
}
2017-05-09
配置Spring.xml文件的<bean>
<!-- 此处Id是该bean的唯一标识,class指定该bean的实现类 -->
<bean id="hello" class="test.HelloImpl"/>
<!-- 此处Id是该bean的唯一标识,class指定该bean的实现类 -->
<bean id="hello" class="test.HelloImpl"/>
2017-05-09
然后是接口的实现类
public class HelloImpl implements Hello{
//私有化接口
private Hello hello;
public HelloImpl(){
}
//Set注入
public void setHello(Hello hello){
this.hello=hello;
}
//重写接口中的say方法
@Override
public String say(String word){
System.out.println("验证此方法是否实现"+word);
return null;
}
}
public class HelloImpl implements Hello{
//私有化接口
private Hello hello;
public HelloImpl(){
}
//Set注入
public void setHello(Hello hello){
this.hello=hello;
}
//重写接口中的say方法
@Override
public String say(String word){
System.out.println("验证此方法是否实现"+word);
return null;
}
}
2017-05-09
这一节主要讲的是Spring通过配置的方式来取代new获取对象。需要对面向接口编程有所了解。
首先写一个接口
public interface Hello{
//再写接口内一个简单方法
String say(String word);
}
首先写一个接口
public interface Hello{
//再写接口内一个简单方法
String say(String word);
}
2017-05-09
否则用
Resource resource = new ClassPathResource("spring-ioc.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Users users = (Users) beanFactory.getBean("users");
这种方法会导致注解失效,TNND,搞了一下午要疯了,得到这么个结果欲哭无泪。
Resource resource = new ClassPathResource("spring-ioc.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Users users = (Users) beanFactory.getBean("users");
这种方法会导致注解失效,TNND,搞了一下午要疯了,得到这么个结果欲哭无泪。
2017-05-09
Bean的获取要用
ApplicationContext context = new ClassPathXmlApplicationContext("spring-ioc.xml");
Users users = (Users) context.getBean("users");
ApplicationContext context = new ClassPathXmlApplicationContext("spring-ioc.xml");
Users users = (Users) context.getBean("users");
2017-05-09