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.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean id="logger" class="com.caveofprogramming.spring.test.Logger">
    </bean>

    <bean id="consoleWriter1" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>

    <context:annotation-config></context:annotation-config>
</beans>

Spring-25 The "Autowired" Annotation

@Autowired   

method,variables,constuctor

Spring-26 Optional Beans

Question : What happened if we use @Autowired(required=false)?


Spring-27 Using qualifiers

Right click ‘consoleWriter‘--> extend ‘bean‘--->Insert <qualifier> element --->setValue ‘toConsole‘

    @Autowired
    @Qualifier("toConsole")
    public void setConsoleWriter(ConsoleWriter writer) {
        this.consoleWriter = writer;
    }

还有一种用法

在method和class前分别添加@Qualifier???

Spring-28  The Resource Annotation(JSR-250)

@Resource

@Resource(name="banana")

这里的关于ambiguity有些问题?????

Spring-29 Annotation-Based Init and Destroy Methods

import.javax.annotation.*

@PostConstruct

@PreDestroy

Spring-30 The Inject Annotation(JSR-330)

javax.inject-1.jar    (pom.xml)

public class Logger {

    private ConsoleWriter consoleWriter;
    private FileWriter fileWriter;

    /*
     * public Logger(ConsoleWriter consoleWriter, FileWriter fileWriter){
     * this.consoleWriter = consoleWriter; this.fileWriter = fileWriter;
     */

    @Inject
    @Named(value="consoleWriter")
    public void setConsoleWriter(ConsoleWriter writer) {
        this.consoleWriter = writer;
    }

    @Inject
    @Named(value="fileWriter")

    public void setFileWriter(FileWriter fileWriter) {
        this.fileWriter = fileWriter;
    }

    public void writeFile(String text) {
        fileWriter.write(text);
    }

    public void writeConsole(String text) {
        if (consoleWriter != null) {
            consoleWriter.write(text);
        }
    }

    @PostConstruct
    public void init(){
        System.out.println("init");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("destroy");
    }

}

Spring-31 Automatic Bean Discovery

    <context:annotation-config></context:annotation-config>
    <context:component-scan
        base-package="com.caveofprogramming.spring.test">
    </context:component-scan>

加上 @Component

为了不存在 ambiguity

[email protected]("fileWriter")

[email protected]

@Named(value="fileWriter")

Spring-32 Setting Property Values via Annotations

@Value("")

时间: 2024-08-02 08:04:40

On the way learning spring 5的相关文章

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); R

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&

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/> · 简单工厂