Spring入门

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

Spring构筑

这里写图片描述
创建出一个XML文件,内容为:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
</beans>

Spring管理Bean

1.创建package com.bdqn.entity

 public class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public  void show(){
        System.out.println(this.getName());
    }
 ```



2.applicationContext.xml配置文件添加bean```
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <bean id="PersonName" class="com.bdqn.entity.Person">
    <property name="name">
    <value>qwertyuiopasdfghjklzxcvbnm</value>
    </property>
    </bean>
</beans>

3.测试类

package com.bdqn.test;

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

import com.bdqn.entity.Person;

public class Text {
    public static void main(String[] args) {
        //加载applicationContext.xml文件
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        //获得文件中的Bean属性
        Person person=(Person) context.getBean("PersonName");
        person.show();
    }
}

IoC容器三种实例化方式 : classpathxmlApplicationcontext resource classpathresource
Bean实例化三种方式 : 构造器 静态工厂 动态工厂
生成了几个实例?共享一个实例 还是存在多个实例?==比较内存地址就知道 scope=”singleton || prototype”>
导入多个XML文件


依赖注入三种方式
setter方式注入 设置属性set方法 通过set属性方法给类属性注入值
构造方法注入 先增加无参数构造方法 然后增加有参数构造方法 通过有参数构造方法里给类属性注入值
P命名空间注入 在bean通过p:方式=”?” value=”值”给类属性注入值 有按顺序 有按类型 有索引 有按属性名 这些方式

IoC容器三种实例化方式

ApplicationContext方式
Resource方式
ClassPathResource方式

<bean id="studentid1" class="com.bdqn.entity.Student"></bean>-->

//IoC容器三种实例化方式一 ApplicationContext方式 默认从根目录src开始读取

 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
 Student student=(Student) applicationContext.getBean("studentid1");
 student.showS();

//IoC容器三种实例化方式二 Resource方式 默认从工程目录下开始读取

 Resource resource=new FileSystemResource("src/applicationContext.xml");
 BeanFactory beanFactory=new XmlBeanFactory(resource);
 Student student2=(Student) beanFactory.getBean("studentid1");
 student2.showS();

//IoC容器三种实例化方式三 ClassPathResource方式 默认从根目录src开始读取

 ClassPathResource resource=new ClassPathResource("applicationContext.xml");
BeanFactory beanFactory=new XmlBeanFactory(resource);
Student student3=(Student) beanFactory.getBean("studentid1");

Student实体类

package com.bdqn.entity;


public class Student {
public void showS(){
System.out.println("学生的showS方法");
}
}

Bean实例化三种方式

构造器实例化 静态工厂方法实例化 实例工厂方法实例化

<bean id="dogid1" class="com.bdqn.entity.Dog"></bean>-->

<bean id="teacherid1" class="com.bdqn.entity.Teacher" factory-method="getTeacherInit"></bean>-->

<bean id="dongTaiGongChang" factory-bean="catid1" factory-method="getCatInit"></bean>
<bean id="catid1" class="com.bdqn.entity.Cat"></bean>-->

//Bean实例化方式一 构造器实例化

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
 Dog dog=(Dog) applicationContext.getBean("dogid1");

//Bean实例化方式二 静态工厂方法实例化

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Teacher teacher=(Teacher) applicationContext.getBean("teacherid1");

//Bean实例化方式三 实例工厂方法实例化

ApplicationContext AC=new ClassPathXmlApplicationContext("applicationContext.xml");
Cat cat=(Cat)AC.getBean("dongTaiGongChang");

实体类Dog

package com.bdqn.entity;


public class Dog {
public Dog(){
System.out.println("Bean实例化构造器方式:"+System.currentTimeMillis());
}
}

实体类Teacher

package com.bdqn.entity;


public class Teacher {
//静态工厂 需要static
public static Teacher getTeacherInit(){
System.out.println("Bean实例化方式二 静态工厂");
return new Teacher();
}
}

实体类Cat

package com.bdqn.entity;


public class Cat {
//注意动态工厂是没有static的
public Cat getCatInit(){
System.out.println("动态工厂"+System.currentTimeMillis());
return new Cat();
}
}

生成了几个实例?

<!--共享一个实例-->
<bean id="bikeid1" class="com.bdqn.entity.Bike" scope="singleton"></bean>

<!--多个实例-->
<bean id="busid1" class="com.bdqn.entity.Bus" scope="prototype"></bean>

//共享一个实例

ApplicationContext AC=new ClassPathXmlApplicationContext("applicationContext.xml");
Bike bike1=(Bike) AC.getBean("bikeid1");
Bike bike2=(Bike) AC.getBean("bikeid1");
System.out.println("共享一个实例内存地址是否相等?"+(bike1==bike2));

//共享一个实例

ApplicationContext AC=new ClassPathXmlApplicationContext("applicationContext.xml");
Bus bus3=(Bus) AC.getBean("busid1");
Bus bus4=(Bus) AC.getBean("busid1");
System.out.println("共享一个实例内存地址是否相等?"+(bus3==bus4));

导入多个.xml文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


<!--导入多个.xml文件-->

<import resource="applicationContextDao.xml"/>
</beans>

依赖注入三种方式

setter方式注入 设置属性set方法 通过set属性方法给类属性注入值
构造方法注入 先增加无参数构造方法 然后增加有参数构造方法 通过有参数构造方法里给类属性注入值P命名空间注入 在bean通过p:方式=”?” value=”值”给类属性注入值 有按顺序注入 有按类型注入 有按索引注入 有按属性名注入 这些方式

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!--p命名空间-->
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="id名字" class="全包名.类名" p:注入方式="?" value="值"></bean>
</beans>

注入 注解 Spring和MyBatis初步整合

一定要记得set方法

<bean id="userid1" class="com.bdqn.entity.User">

  <!--普通量 基础类型 注入-->
  <property name="uName">
   <value>lizihao</value>
  </property>


  <!--内部bean 就是说不是基础类型 是引用类型 记得第二个bean也要属性才能赋值-->
  <property name="student">
   <bean class="com.bdqn.entity.Student">
    <property name="sName">
     <value>学生</value>
    </property>
   </bean>
  </property>


  <!--list集合注入-->
  <property name="list">
   <list>
    <value>diyige</value>
    <value>dierge</value>
    <value>jojhio</value>
   </list>
  </property>


  <!--list集合加泛型注入 主要是ref bean-->
  <property name="studentList">
   <list>
    <ref bean="studentid1" />
    <ref bean="studentid2" />
   </list>
  </property>


  <!--set集合注入-->
  <property name="set">
   <set>
    <value>set1</value>
    <value>set2</value>
    <value>set3</value>
    <value>set2</value>
    <value>set4</value>
   </set>
  </property>


  <!--数组注入-->
  <property name="str">
   <list>
    <value>shuzuyi</value>
    <value>shuzuer</value>
    <value>shuzusan</value>
    <value>shuzusi</value>
    <value>shuzuwu</value>
    <value>shuzuliu</value>
   </list>
  </property>



  <!--map集合注入 主要的是entry-->
  <property name="map">
   <map>
    <entry>
     <key> <value>liubei</value> </key>
     <value>liuxuande</value>
    </entry>
    <entry>
     <key> <value>guanyu</value> </key>
     <value>guanyuanchang</value>
    </entry>
    <entry>
     <key> <value>zhangfei</value> </key>
     <value>zhangyide</value>
    </entry>
   </map>
  </property>

 <bean id="studentid1" class="src.com.bdqn.entity.Student"></bean>
 <bean id="studentid2" class="src.com.bdqn.entity.Student"></bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值