吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2509|回复: 8
收起左侧

[Java 转载] Spring框架基础篇【一】

[复制链接]
小辣椒大佬 发表于 2018-3-14 11:17
class  spring


/*------Spring容器实例化------*/
{
//加载工程classpath下的配置文件实例化
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);

/*------Spring容器的使用------*/
//首先在容器配置文件ApplicationContext.xml中添加Bean定义
<bean id="标识符" class="Bean类型"/>
//然后在创建BeanFactory和ApplicationContext容器对象后,调用
//getBean()方法获取Bean实例    getBean("标识符")


/*------实例化Spring容器------*/
//使用ApplicationContext的方式实例化Spring容器
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);


/*
*------Bean的实例化------
*Spring容器创建Bean对象的方法:
*用构造器来实例化
*使用静态工厂方法
*使用实例工厂方法
*/
//使用Spring容器创建bean  在applicationContext.xml中,通过<bean>声明bean
<beans>
<!--通过构造器实例化bean -->
    <bean id="obj1" class="java.util.GregorianCalendar"/>
   
    <!--通过静态工厂方法实例化bean -->
    <bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
   
    <!--通过实例工厂方法实例化bean -->
    <bean id="obj3" class="java.util.GregorianCalendar"/>
    <bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
</beans>
//测试方法:
public class TestCase(){
@Test
public void test(){
String cfg = "applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
Calendar cal1 = (Calendar)ctx.getBean("obj1");
System.out.println(cal1);
Calendar cal2 = ctx.getBean("obj2",Calendar.class);
System.out.println(cal2);
Date date = ctx.getBean("obj4",Date.class);
System.out.println(date);
}
}


/*----bean的作用域----*/
//声明bean 在applicationContext.xml中,追加一个bean声明
<!--  bean的作用域 -->
<bean id = "obj5" class="java.util.GregorianCalendar"/>
//测试:
@Test
public void test(){
String cfg = "applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContex(cfg);
Calendar cal1 = (Calendar)ctx.getBean("obj5");
Calendar cal2 = (Calendar)ctx.getBean("obj5");
System.out.println(cal1==cal2);//true
}
//修改bean声明
<!--  bean的作用域 -->
<bean id = "obj5" class="java.util.GregorianCalendar" scope="prototype"/>
//测试,执行上述测试输出为:false


/*----bean的生命周期----*/
//创建bean
package com.shine.bean;
import java.io.Serializable;
public class ExampleBean implements Serializable{
public ExampleBean() {
        System.out.println("实例化ExampleBean:" + this);
    }
   
    public void init() {
        System.out.println("初始化ExampleBean");
    }
   
    public void destroy() {
        System.out.println("销毁ExampleBean");
    }
   
    public void execute() {
        System.out.println("执行execute方法");
    }
}
//声明bean
<!-- bean的生命周期 -->
    <!-- bean的延迟实例化 -->
    <bean id="exampleBean" class="com.tarena.bean.ExampleBean"
        init-method="init" destroy-method="destroy" />
//测试
/**
     * 1.bean的生命周期;
     * 2.bean的延迟实例化;
     */
    @Test
    public void test() {
        String cfg = "applicationContext.xml";
        AbstractApplicationContext ctx =
            new ClassPathXmlApplicationContext(cfg);
        System.out.println("----------------");
        ExampleBean bean =
            ctx.getBean("exampleBean", ExampleBean.class);
        bean.execute();
        ctx.close();
    }




/*----IOC/控制反转----*/
/*IOC是指程序中对象的获取方式发生反转,又最初的new方式创建
转变为由第三方框架创建,注入(DI),他降低了对象之间的耦合度*/
/*DI:依赖注入
基本原理就是将一起工作具有关系的对象,通过构造方法参数或方法
参数传入建立关联,因此容器的工作就是创建bean时注入那些依赖关系*/
/*IOC是一种思想,DI是实现IOC的主要技术途径*/
/*DI主要有两种注入方式,Setter注入和构造器注入*/
//setter注入
package com.shine.bean;
import java.io.Serializable;
public class Computer implements Serializable {
    private String mainboard; // 主板
    private String hdd; // 硬盘
    private String ram; // 内存
    public String getMainboard() {
        return mainboard;
    }
    public void setMainboard(String mainboard) {
        this.mainboard = mainboard;
    }
    public String getHdd() {
        return hdd;
    }
    public void setHdd(String hdd) {
        this.hdd = hdd;
    }
    public String getRam() {
        return ram;
    }
    public void setRam(String ram) {
        this.ram = ram;
    }
}
//声明bean;在applicationContext.xml中声明这个bean
<!-- setter注入 -->
    <bean id="computer" class="com.shine.bean.Computer">
        <property name="mainboard" value="技嘉"/>
        <property name="hdd" value="希捷"/>
        <property name="ram" value="金士顿"/>
    </bean>
//测试
/**
     * setter注入
     */
    @Test
    public void test() throws SQLException {
        String cfg = "applicationContext.xml";
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
        Computer computer = ctx.getBean("computer", Computer.class);
        System.out.println(computer.getMainboard());
        System.out.println(computer.getHdd());
        System.out.println(computer.getRam());
    }
//构造器注入
//创建bean;创建一个手机类MobilePhone
package com.shine.bean;
import java.io.Serializable;
public class MobilePhone implements Serializable {
    private String cpu;
    private String ram;
    public MobilePhone(String cpu, String ram) {
        this.cpu = cpu;
        this.ram = ram;
    }
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
    public String getRam() {
        return ram;
    }
    public void setRam(String ram) {
        this.ram = ram;
    }
}
//声明bean;在applicationContext.xml中声明这个bean
<!--构造器注入 -->
    <bean id="phone" class="com.shine.bean.MobilePhone">
        <constructor-arg index="0" value="ARM"/>
        <constructor-arg index="1" value="2G"/>
    </bean>
//测试
/**
     * 构造器注入
     */
    @Test
    public void test() throws SQLException {
        String cfg = "applicationContext.xml";
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
        MobilePhone phone = ctx.getBean("phone", MobilePhone.class);
        System.out.println(phone.getCpu());
        System.out.println(phone.getRam());
    }


/*自动装配*/
//通过Spring自动装配机制,自动为一个bean装配其关联的bean
//采用autowire="byType",即按照bean的类型进行自动装配
    //创建bean;创建一个学生类Student,该类中有计算机、手机属性,即学生关联了计算机和手机
package com.shine.bean;
import java.io.Serializable;
public class Student implements Serializable {
    private Computer computer;
    private MobilePhone mobilePhone;
    public Computer getComputer() {
        return computer;
    }
    public void setComputer(Computer computer) {
        this.computer = computer;
    }
    public MobilePhone getMobilePhone() {
        return mobilePhone;
    }
    public void setMobilePhone(MobilePhone mobilePhone) {
        this.mobilePhone = mobilePhone;
    }
}
//声明bean;在applicationContext.xml中声明这个bean
<!--自动装配 -->
    <bean id="student" class="com.shine.bean.Student" autowire="byType">
    </bean>
//测试
public void test() throws SQLException {
        String cfg = "applicationContext.xml";
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
        Student student = ctx.getBean("student", Student.class);
        System.out.println(student.getComputer());
        System.out.println(student.getMobilePhone());
    }


}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
wushaominkk + 1 + 1 鼓励新人多发帖,吾爱因您更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

netCheney 发表于 2018-3-14 11:54
这个是开发安卓的吗?对spring没有任何了解,只是看语言像是C
天地皇极 发表于 2018-3-14 15:53
 楼主| 小辣椒大佬 发表于 2018-3-19 11:18
netCheney 发表于 2018-3-14 11:54
这个是开发安卓的吗?对spring没有任何了解,只是看语言像是C

安卓开发也会用到spring,有点用处的
 楼主| 小辣椒大佬 发表于 2018-3-19 11:19
天地皇极 发表于 2018-3-14 15:53
收藏了,最近学习Java刚学到框架。

这些都是spring的基础,希望对你有点帮助
wushaominkk 发表于 2018-3-19 11:21
鼓励新人多发帖,吾爱因您更精彩!
 楼主| 小辣椒大佬 发表于 2018-3-19 11:22
wushaominkk 发表于 2018-3-19 11:21
鼓励新人多发帖,吾爱因您更精彩!

谢谢版主大大
netCheney 发表于 2018-3-19 18:07
小辣椒大佬 发表于 2018-3-19 11:18
安卓开发也会用到spring,有点用处的

哦,看了楼下知道原来这个是java的,Java好难搞的,谢谢楼主了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 13:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表