Spring框架:接口,了解bean的作用域,spring容器,setter注入,自动装配【诗书画唱】
概括:
举例
个人对Spring好处的理解和修改部分数据后要跟着改的内容
个人注意事项记录
告诉你什么是setter注入(引用注入)
把这些关联大的关系称为“耦合”。spring框架的使用的作用之一为“松耦合”。
contains包含
spring容器中常用的方法。
bean的作用域
自动装配
自动装配可以减少原先要写配置文件的代码
3、SPRINGCORE框架_Spring容器.ppt
4、SPRINGCORE框架_了解Bean.ppt
使用setter注入,byName自动装配等方法调用类的方法的代码例子
public interface Track
个人理解:setter注入和构造器注入等都有不同的类型,比如值注入,引用注入等等



举例
个人对Spring好处的理解和修改部分数据后要跟着改的内容:
其实如果要去引入和使用导入别人写的Spring项目的话,需要修改的文件有:配置文件等等。有时自己可以修改下包名等,之后配置文件也对应地修改。其实修改内容时,基本只要注意配置文件的设置等等。这些就是我理解的Spring框架的好处之一。

其实如果要去引入和使用导入别人写的Spring项目的话,需要修改的文件有:配置文件等等。有时自己可以修改下包名等,之后配置文件也对应地修改。其实修改内容时,基本只要注意配置文件的设置等等。这些就是我理解的Spring框架的好处之一。

个人注意事项记录
告诉你什么是setter注入(引用注入)








把这些关联大的关系称为“耦合”。spring框架的使用的作用之一为“松耦合”。

contains包含



spring容器中常用的方法。







bean的作用域




自动装配。








使用setter注入,byName自动装配等方法调用类的方法的代码例子 START

package com.SSHC.bean;
public class Bike implements Track {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("踩踏运载(打印出这句话说明成功调用"
+ "Bike类中的run()方法)");
}
}

package com.SSHC.bean;
public class Car implements Track{
@Override
public void run(){
System.out.println("四轮运载,打印出这句话,说明调用了"
+ "Track类中的run方法");
}
}

package com.SSHC.bean;
public class Cat {
private String name;
private Integer age;
public Cat(){
}
public Cat(String n,Integer a) {
this.name = n;
this.age = a;
}
public void play(){
System.out.println("正在玩耍,Cat类中含有的abc方法"
+ "\n 因为default-init-method被调用了\n \n ");
}
public void abc(){
System.out.println("初始猫开始...,Cat类中含有的abc方法"
+ "\n 因为default-init-method被调用了\n \n ");
}
}

package com.SSHC.bean;
public class Dog {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字是:" + this.name + ",年龄是:" + this.age;
}
}

package com.SSHC.bean;
public class Person {
private String name;//值类型
private Cat pet;//引用类型
public Person(String n,Cat c){
this.name = n;
this.pet = c;
}
public void guanging(){
this.pet.play();
}
public void abc(){
System.out.println("初始化人开始...Person类中含有的abc方法"
+ "\n 因为default-init-method被调用了\n \n ");
}
}

package com.SSHC.bean;
public class Plane implements Track {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("空中运载");
}
}

package com.SSHC.bean;
public class ProductDao {
public void add(){
System.out.println("ProductDao中的add新增方法被调用了!");
}
}

package com.SSHC.bean;
public class Student {
private String name;//值
private Dog dog;//引用
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void play(){
System.out.println("正在跟" +
this.dog.getName() + "玩耍,"
+ "\n 调用了Student类中的play方法"
+ "\n");
}
}

package com.SSHC.bean;
//交通工具接口
public interface Track {
//交通工具的运载方式
void run();
}

package com.SSHC.bean;
public class UserDao {
public void selectAll(){
System.out.println("查询所有,调用了UserDao中的selectAll方法");
}
}

package com.SSHC.bean;
public class UserService {
private UserDao userDao;
private ProductDao productDao;
private Person p;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void setP(Person p) {
this.p = p;
}
public void loadData(){
userDao.selectAll();
productDao.add();
p.guanging();
}
}

package MainText;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Cat;
import com.SSHC.bean.Dog;
import com.SSHC.bean.Person;
import com.SSHC.bean.Student;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//new关键字不要出现在代码中
//Cat c = new Cat();
//Cat c1 = new Cat("大黄",1);
//获取spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
while(true){
/*配置文件中beans标签中
* 设置了 default-init-method="abc"
* ,那么针对全部bean,如果有abc的方法,
* 创建这个bean时就会调用一次这个方法。*/
System.out.print("请选择操作:"
+ "1.setter注入,Dog测试(配置文件中设置了value值) "
+ ",2.setter注入,Student测试,"
+ "\n 3.用Spring框架调用Cat中的方法,"
+ "4.用Spring框架调用Person中的方法,"
+ "\n"
+ "5.打印ctx.getType(XXX)方法");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num==1){
// Dog d = new Dog();
// d.setName("诗书画唱养的的狗狗");
// d.setAge(6);
Dog d = (Dog) ctx.getBean("d");
System.out.println(d);
}
if(num==2){
// Student s = new Student();
// s.setName("小华");
// s.setDog(d);
Student s = (Student) ctx.getBean("s");
s.play();
}
if(num==3){
Cat c1 = (Cat) ctx.getBean("c1");
c1.play();
}
if(num==4){
// //Person p = new Person("小明",c1);
Person p = (Person) ctx.getBean("p");
p.guanging();
}
if(num==5){
System.out.println(ctx.getType("s"));}
}
}
}

package MainText;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Cat;
import com.SSHC.bean.Dog;
import com.SSHC.bean.Person;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//new关键字不要出现在代码中
//Cat c = new Cat();
//Cat c1 = new Cat("大黄",1);
//获取spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
Cat c1 = (Cat) ctx.getBean("c1");
c1.play();
// //Person p = new Person("小明",c1);
Person p = (Person) ctx.getBean("p");
p.guanging();
// Dog d = new Dog();
// d.setName("旺旺");
// d.setAge(2);
}
}

package MainText;
import java.util.Scanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.SSHC.bean.Bike;
import com.SSHC.bean.Car;
import com.SSHC.bean.Cat;
import com.SSHC.bean.Track;
import com.SSHC.bean.UserService;
public class Test {
public static void main(String[] args) {
while(true){
System.out.print("请选择操作:"
+ "1.使用Spring框架后不使用的new,"
+ "\n"
+ "import关键字的调用Bike,Cat类中的方法"
+ ",\n 2.Spring框架调用plane中的run方法"
+ "\n 3.使用containsBean判断applicationContext.xml文件"
+ "中\n是否包含id值为c1的bean标签的部分,"
+ "\n打印true就说明存在,false就是不存在,"
+ " "
+ "\n 4.使用Spring框架中的byName的自动装配"
+ "\n 来调用userService中的方法"
+ "\n");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num==1){
Bike b = new Bike();
b.run();
Car c = new Car();
c.run();
}
if(num==2){
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
Track t = (Track) ctx.getBean("track");
/*配置文件中
* <bean id="track"
* class="com.SSHC.bean.Plane"></bean>
* 所以,调用的是plane中的run方法*/
t.run();
}
if(num==3){
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ctx.containsBean("c1"));
//contains:包含
}
if(num==4){
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService =
(UserService) ctx.getBean("userService");
userService.loadData();
}}}
}

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
default-init-method="abc">
<bean id="c" class="com.SSHC.bean.Cat"></bean>
<bean id="c1" class="com.SSHC.bean.Cat" scope="prototype">
<constructor-arg value="大黄"></constructor-arg>
<constructor-arg value="1"></constructor-arg>
</bean>
<bean id="p" class="com.SSHC.bean.Person">
<constructor-arg value="小明"></constructor-arg>
<constructor-arg ref="c1"></constructor-arg>
</bean>
<bean id="d" class="com.SSHC.bean.Dog">
<property name="name" value="诗书画唱养的的狗狗"></property>
<property name="age" value="2"></property>
</bean>
<bean id="s" class="com.SSHC.bean.Student">
<property name="name" value="小华"></property>
<property name="dog" ref="d"></property>
</bean>
<bean id="track" class="com.SSHC.bean.Plane"></bean>
<bean id="userDao" class="com.SSHC.bean.UserDao"></bean>
<bean id="productDao" class="com.SSHC.bean.ProductDao"></bean>
<bean id="userService" class="com.SSHC.bean.UserService"
autowire="byName">
<!-- 在进行setter注入时,如果name和ref相同,那么就可以进行byName自动装备 -->
<!-- <property name="userDao" ref="userDao"></property> -->
</bean>
</beans>

在bean包中的实体类(比如Bike类)的话就是会声明一个方法(比如Bike类中的run的方法,方法
声明了一个打引语句,打印成功,声明调用这个方法成功了)
。以前调用这个方法的话,就要在含main方法中的class中new出这个方法所在位置的class的类,这样
的话就还要,使用import关键导入这个类的包。
但是使用了Spring框架的话,就是只要在applicationContext.xml这个应用上下文(这个名字写出了Spring类似
框架连接的桥梁的作用)的xml配置文件中
使用bean标签,设置其中的项目中唯一值的id等等。
之后用代码获取这个applicationContext.xml
代码运行结果和说明:






使用setter注入,byName自动装配等方法调用类的方法的代码例子
END
3、SPRINGCORE框架_Spring容器.ppt START





SPRINGCORE框架
Spring容器
Spring上下文(一)
Spring容器最基本的接口就是BeanFactory。该类负责配置、创建和管理Bean,它有一个名为applicationContext的子接口,称为Spring上下文。 BeanFactory包含如下的几种方法:
Spring上下文(二)
ApplicationContext是BeanFactory的一个子接口,在一般的JAVA应用程序中,常用的实现类有以下几种:1、FileSystemXmlApplicationContext2、ClassPathXmlApplicationContext3、AnnotationConfigApplicationContext 另外,如果需要在web开发中使用Spring容器,则需要使用以下的几种实现类:1、XmlWebApplicationContext2、AnnotationConfigWebApplicationContext
Java应用程序中的Spring容器
对于独立的Java应用程序,必须提供Spring容器管理的Bean的详细配置信息,而Spring的配置信息通常采用XML文件来进行管理,因此可以通过BeanFactory的实现类XmlBeanFactory来实例化BeanFactory,而XML文件将被作为参数传入。XML配置文件通常使用Resource对象传入。 1、创建一个非web工程,导入spring相关包。 2、在工程根目录下创建文件bean.xml。 3、创建类DemoBean及其方法sayHi,该方法负责打印hi 4、创建测试类Test,包含main方法。
JavaWeb程序中的Spring容器
对于JavaWeb程序,对XML文件进行解析的工作应该在工程启动时进行,可以通过配置由Spring框架自带的监听类ContextLoaderListener来实现。 注意:这个监听类实际上也可以自己定义,但需要xml的某种解析模式以及反射技术的配合才可以完成。可通过查看org.springframework.web.context.ContextLoaderListener源码进行了解。
3、SPRINGCORE框架_Spring容器.ppt END
4、SPRINGCORE框架_了解Bean.ppt START











SPRINGCORE框架
了解Bean
Bean的基本定义(一)
在Spring的配置文件中,<beans>元素是作为根元素出现的,可以指定以下的属性。
Bean的基本定义(二)
在Spring的配置文件中,<bean>元素是<beans>元素的子元素,每个<bean>元素都代表定义了一个类。<bean>元素可以指定以下的属性
Bean的基本定义(三)
定义<bean>时,通常需要指定两个属性。1、id:确定Bean的唯一标识符,容器对Bean管理、访问以及指定Bean之间的依赖关系,都通过该属性完成。在每个spring容器中,都是唯一的。2、class:指定Bean的具体实现类全路径名。
设置BEAN的作用域
所有的Spring Bean默认都是单例的,当容器分配一个Bean时,无论是通过装配还是调用容器的getBean方法,总是返回同一个Bean的实例,如果需要改变这种情况,可以通过设置bean的作用域scope属性来覆盖Bean的单例配置。
使用自动装配注入Bean(一)
Spring能自动装配Bean与Bean之间的依赖关系,即无须使用ref显式指定依赖的Bean。由BeanFactory检查XML配置文件的内容,根据某种规则,为主调Bean注入依赖关系。自动装配可通过指定<beans>元素的default-autowire或者<bean>元素的autowire属性指定。 注意:自动装配能够减少配置文件的工作量,但是降低了依赖关系的透明性和清晰性。autowire属性包含以下几种值
使用自动装配注入Bean(二)
创建如下的类:
使用自动装配注入Bean(三)
byName:
1、查找class指定类中所有的set方法名,获得将set去掉并且首字母小写的字符串。
2、在Spring容器中寻找id为此字符串名称的Bean对象。
3、如果有,就进行注入;如果没有,就报空指针异常。
使用自动装配注入Bean(四)
byType:
首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常
使用自动装配注入Bean(五)
1、创建UserDao类,创建PubService类,在PubService类中包含一个私有的UserDao类型的成员属性以及setter。2、修改配置文件,通过pubService的autowire="byName"进行自动装配。3、修改配置文件 ,通过pubService的autowire="byType"进行自动装配。4、在PubService类中,添加一个构造方法,该构造方法包含一个UserDao类型的参数。5、修改配置文件,通过pubService的autowire="constructor"进行自动装配。注意:显式的依赖注入将覆盖自动装配。