On the way learning spring 2

Spring 10 - Bean scope

Prototype; Request ;Session; Singleton.

Source Code:

Person person1 =  (Person) context.getBean("person");
Person person2 =  (Person) context.getBean("person");

person1.setTaxId(666);

System.out.println(person2);

Result:

Person [id=777, name=Mary, taxId=666, address=Address [street=hillside, pos=08540]]

Default behave(Singleton): No matter how many times you request this beans from the container, will always be given the same bean.

Change scope to prototype

<bean id="person"
class="com.caveofprogramming.spring.test.Person"
        scope="prototype">

Result:

Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]

Spring 11 - Init and Destroy Methods

1.Add method onCreate() in Person.class.

public void onCreate(){
        System.out.println("Person created: " + this);
    }

2.Set Init-method in beans.xml  to  onCreate

Result:

Person created: Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]
Person created: Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]
Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]

3.Add method onDestroy() in Person.class

    public void onDestroy(){
        System.out.println("Person destroyed.");
    }

4.Set destroy-method in beans.xml to toDestroy

Result:

Person created: Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]
Person created: Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]
Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]

5. Set scope back to singleton(default)

Result:

Person created: Person [id=777, name=Mary, taxId=123, address=Address [street=hillside, pos=08540]]
Person [id=777, name=Mary, taxId=666, address=Address [street=hillside, pos=08540]]
Person destroyed.
时间: 2024-11-06 02:00:42

On the way learning spring 2的相关文章

On the way learning spring 3

Spring-12 Factory Beans and Methods static method in Class person non-static method in personFactory Spring-13 The P Namespace 注意Constructor 中的Arg 和建立的Bean之间的关系 p: tag 和 property tag 不要同时使用 Spring-14 Setting List Property In beans.xml file Insert con

On the way learning spring 4

Spring-19 Auto-Wiring  by type 在Xml文件中定义bean之间的关系时,有3种方法: 1. inner bean 2. 在bean 的property中定义另外一个bean 3. Spring Autowiring(有很多种类型, 这里 by type) <bean id="logger" class="com.caveofprogramming.spring.test.Logger" autowire="byType&

On the way learning spring 5

Spring-24 Adding Support for Annotation-Based Wiring Namespace-->check 'context'-->In 'context' Insert <context:annotation-config> element <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spring

learning spring for Init Project (一)

reference:https://spring.io/quickstart 内容概要: 1 .初始化工程: 首先打开Idea软件, File->New->Project,如下图所示: 选中上图Spring Assistant,这是一个插件用来初始化spring工程.点击上图下一步,如下图所示: 点击Next.如下图所示: 选中上图的Web->Spring Web,然后点击Next,如下图所示: 输入项目名称,选项工程存放路径,点击Next. 如下图所示: 等待工程自动下载相关的jar包

On the way learning spring 6

Spring-47 Adding an Update Method to the DAO public boolean update(Offer offer){BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(offer); return jdbc.update("update offers set name=:name,text=:text,email=:email where id =:id&

【深度学习Deep Learning】资料大全

转载:http://www.cnblogs.com/charlotte77/p/5485438.html 最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books Deep Learning66 by Yoshua Bengio, Ian Goodfellow and Aaron Courville Neural Networks and Deep Learning42 by Michael Nielsen Deep Learning27 by

【转】The most comprehensive Data Science learning plan for 2017

I joined Analytics Vidhya as an intern last summer. I had no clue what was in store for me. I had been following the blog for some time and liked the community, but did not know what to expect as an intern. The initial few days were good – all the in

DEEP LEARNING 大满贯课程表

Reinforcement Learning post by ISH GIRWAN Courses/Tutorials Deep Reinforcement Learning, Spring 2017, by UC Berkeley: http://rll.berkeley.edu/deeprlcours... Reinforcement Learning, 2015, by UCL (David Siver): http://www0.cs.ucl.ac.uk/staff/d.si... ht

Spring的配置分别是xml和java style

# Spring-IoCSpring配置有两种方式xml和java style ## Spring · spring的IoC(控制反转)就是一个工厂模式变种,<br/> · spring核心就是IoC容器,实现这个容器的接口BeanFactory,BeanFactory是最核心的.最纯粹的<br/> 方法: <br/> getBean() <br/> 实现类: <br/> ApplicationContext <br/> · 简单工厂