热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SPRINGMYBATIS01Unit02:参数值注入、基于注解的组件扫描

1.IOCDI(1)什么是IOC(InversionOfControll控制反转)对象之间的依赖关系由容器来建立。(2)什么是DI(Depen

1. IOC/DI

(1)什么是IOC(Inversion Of Controll 控制反转)

对象之间的依赖关系由容器来建立。

(2)什么是DI(Dependency Injection 依赖注入

容器可以通过调用set方法或者构造器来建立对象之间的依赖关系。
注:
IOC是目标,DI是手段。

这里写图片描述

(3)依赖注入的两种方式

1)方式一  set方法注入
step1. 添加set方法
step2. 在配置文件中,使用元素来配置。

这里写图片描述

2)方式二 构造器注入
step1. 添加构造器
step2. 在配置文件当中,使用元素来配置。

这里写图片描述

(4)自动装配 (了解)

注:自动装配,指的是容器依据某些规则,自动建立对象之间的依赖关系。
1)默认情况下,容器不会自动装配。
2)设置autowire属性

这里写图片描述

(5) 注入基本类型的值

使用value属性来注入,spring容器会帮我们做一些类型的转换工作,
比如将字符串转换成数字。

(6) 注入集合类型的值 (List,Set,Map,Properties)

方式一 直接注入

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

方式二 引用的方式注入
step1. 将集合类型的值先配置成一个bean。
step2. 再将这个bean注入到对应的bean里面。

这里写图片描述


代码演示:

这里写图片描述

/spring02/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>welkingroupId>
<artifactId>spring02artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>3.2.8.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>

/spring02/src/main/java/ioc

A.java

package ioc;

public class A {
private IB b;

public void setB(IB b) {
System.out.println("A's setB()");
this.b = b;
}

public A() {
System.out.println("A()");
}

public void service(){
System.out.println("A's service()");
//调用B的f1()方法
b.f1();
}

}

B.java

package ioc;

public class B implements IB{

public B(){
System.out.println("B()");
}

public void f1(){
System.out.println("B's f1()");
}
}

C.java

package ioc;

public class C implements IB {

public void f1() {
System.out.println("C's F1()");

}

public C() {
System.out.println("C()");
}


}

IB.java

package ioc;

public interface IB {
public void f1();
}

Computer .java

package ioc;

public class Computer {

public Computer() {
System.out.println("Computer()");
}

}

Maneger.java

package ioc;

public class Maneger {
private Computer cp;

public Maneger() {
System.out.println("Maneger()");
}

public Maneger(Computer cp) {
System.out.println("Manager(cp)");
this.cp = cp;
}

@Override
public String toString() {
return "Maneger [cp=" + cp + "]";
}

}

/spring02/src/main/java/AutoWire

Bar.java

package AutoWire;

public class Bar {
private Foo foo;

public Bar() {
System.out.println("Bar()");
}

public void setFoo(Foo foo) {
System.out.println("setFoo()");
this.foo = foo;
}

@Override
public String toString() {
return "Bar [foo=" + foo + "]";
}

}

Foo.java

package AutoWire;

public class Foo {

public Foo() {
System.out.println("Foo()");
}

}

/spring02/src/main/java/value

ExampleBean.java

package value;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ExampleBean {
private List interest;
private Set city;
private Map score;
private Properties db;

@Override
public String toString() {
return "ExampleBean [interest=" + interest + ", city=" + city + ", score=" + score + ", db=" + db + "]";
}

public List getInterest() {
return interest;
}

public void setInterest(List interest) {
this.interest = interest;
}

public Set getCity() {
return city;
}

public void setCity(Set city) {
this.city = city;
}

public Map getScore() {
return score;
}

public void setScore(Map score) {
this.score = score;
}

public Properties getDb() {
return db;
}

public void setDb(Properties db) {
this.db = db;
}


}

InfoBean.java

package value;

public class InfoBean {
private String name;
private String interest;
private double score;
private int pageSize;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

@Override
public String toString() {
return "InfoBean [name=" + name + ", interest=" + interest + ", score=" + score + ", pageSize=" + pageSize
+ "]";
}


}

ValueBean.java

package value;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ValueBean {
private String name;
private int age;
private List interest;
private Set city;
private Map score;
private Properties db;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getInterest() {
return interest;
}
public void setInterest(List interest) {
this.interest = interest;
}
public Set getCity() {
return city;
}
public void setCity(Set city) {
this.city = city;
}
public Map getScore() {
return score;
}
public void setScore(Map score) {
this.score = score;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}

@Override
public String toString() {
return "ValueBean [name=" + name + ", age=" + age + ", interest=" + interest + ", city=" + city + ", score="
+ score + ", db=" + db + "]";
}


}

/spring02/src/main/resources

autowire.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
>

<bean id="foo" class="AutoWire.Foo"/>

<bean id="bar" class="AutoWire.Bar" autowire="byType" />

beans>

config.properties

pageSize=10

ioc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
>


<bean id="b1" class="ioc.B" />

<bean id="a1" class="ioc.A" >

<property name="b" ref="c1"/>
bean>

<bean id="c1" class="ioc.C"/>

<bean id="cp1" class="ioc.Computer"/>
<bean id="mg1" class="ioc.Maneger">

<constructor-arg index="0" ref="cp1"/>
bean>
beans>

value.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
>


<bean id ="ib1" class="value.InfoBean">
<property name="name" value="#{vb1.name}"/>
<property name="interest" value="#{vb1.interest[1]}"/>
<property name="score" value="#{vb1.score['english']}"/>
<property name="pageSize" value="#{config.pageSize}"/>
bean>


<bean id="vb1" class="value.ValueBean">
<property name="name" value="关关" />
<property name="age" value="22" />
<property name="interest">
<list>
<value>台球value>
<value>钓鱼value>
<value>看电视value>
<value>看电视value>
list>
property>
<property name="city">
<set>
<value>北京value>
<value>上海value>
<value>武汉value>
<value>武汉value>
set>
property>
<property name="score">
<map>
<entry key="english" value="70" />
<entry key="math" value="90" />
map>
property>
<property name="db">
<props>
<prop key="username">Sallyprop>
<prop key="password">1234prop>
props>
property>
bean>



<util:list id="interestBean">
<value>喝酒value>
<value>烫头value>
<value>抽烟value>
util:list>
<util:set id="cityBean">
<value>长沙value>
<value>岳阳value>
<value>华容value>
util:set>
<util:map id="scoreBean">
<entry key="math" value="80" />
<entry key="english" value="59.5" />
util:map>
<util:properties id="dbBean">
<prop key="username">Givingprop>
<prop key="password">testprop>
util:properties>


<bean id="eb1" class="value.ExampleBean">
<property name="interest" ref="interestBean" />
<property name="city" ref="cityBean" />
<property name="score" ref="scoreBean" />
<property name="db" ref="dbBean" />
bean>



<util:properties id="config" location="classpath:config.properties" />


beans>

/spring02/src/test/java/test/TestCase.java

package test;

import java.util.Properties;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import AutoWire.Bar;
import ioc.A;
import ioc.Maneger;
import value.ExampleBean;
import value.InfoBean;
import value.ValueBean;

public class TestCase {
@Test
// 测试 set方法注入
public void test1() {
String cOnfig= "ioc.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
A a = ac.getBean("a1", A.class);
a.service();
}

@Test
// 测试 构造器注入
public void test2() {
String cOnfig= "ioc.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Maneger mg1 = ac.getBean("mg1", Maneger.class);
System.out.println(mg1);
}

@Test
// 测试 自动装配
public void test3() {
String cOnfig= "autowire.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Bar bar = ac.getBean("bar", Bar.class);
System.out.println(bar);
}

@Test
// 测试 注入基本类型的值
public void test4() {
String cOnfig= "value.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
ValueBean vb1 = ac.getBean("vb1", ValueBean.class);
System.out.println(vb1);
}

@Test
// 测试 集合类型的值的注入
public void test5() {
String cOnfig= "value.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
ExampleBean eb = ac.getBean("eb1", ExampleBean.class);
System.out.println(eb);
}

@Test
// 测试 读取属性文件的内容
public void test6() {
String cOnfig= "value.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Properties props = ac.getBean("config", Properties.class);
System.out.println(props);
}

@Test
//测试 spring表达式
public void test7(){
String cOnfig= "value.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
InfoBean ib1 = ac.getBean("ib1",InfoBean.class);
System.out.println(ib1);
}
}

推荐阅读
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
author-avatar
偶们滴小圈子6868
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有