Spring MVC 复习一之 Bean 的基本操作

Bean 的属性:

      1、class:是强制的,用来创建指定bean的bean类。*(路径为包名.类名)
         id:名字。*(唯一性)
      2、name:用来指定标识符。*(需与创建类变量保持一致)
      3、scop:是特定的bean定义创建对象的作用域。*(如public)
      **4**、constructor-arg:他是依赖注入里的构造器注入里的一个标签。
      **5**、properties:用于bean与bean的连接,以及设置属性。
      **6**、autowiring mode:用于bean的setter和非setter方法构造函数和属性。
       4、5、6用于注入依赖关系。

Bean的作用域:

1、singleton 将bean的定义的限制在每一个springIOC中,——单一实例。
2、prototype将单一bean的定义在任意数量的对象实例,——多项实例。

简单代码操作:

  1. 简单方法类 :A01

package FxBeanJBCZ01;
import java.text.DecimalFormat;
import java.text.Format;

public class A01 {
public void aa(){
int aa=01;
Format a=new DecimalFormat(“00”);
//这个方法就是可以吧int类型01给在控制台打印出来
System.out.println(a.format(01));
}
}

  1. 第二 测试类: Bsyso

package FxBeanJBCZ01;
import java.text.DecimalFormat;
import java.text.Format;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Bsyso {
public A01 aa;
public A01 getAa() {
return aa;
}
public void setAa(A01 aa) {
this.aa = aa;
}
public static void main(String[] args) {
ApplicationContext aa=new ClassPathXmlApplicationContext(“fxbean01.xml”);
//创建容器对象并指定xml文件
Bsyso wc=(Bsyso) aa.getBean(“d”);
//对象aa调用getbean方法指定bean赋值给wc。
wc.aa.aa();
//对象wc调用aa类变量aa的类方法aa();
}
}

  1. xml文件配置:
    需要导入一些jar包
    E:\STS\JAR包\commons-logging-1.2.jar
    E:\STS\JAR包\spring-aop-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-beans-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-context-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-core-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-expression-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-web-4.3.5.RELEASE.jar
    E:\STS\JAR包\spring-webmvc-4.3.5.RELEASE.jar
    其中只用到beans context两个其余的在以后用到
    < ?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
    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.3.xsd”>
    < bean id=“a” class=“FxBeanJBCZ01.A01”>< /bean>
    < bean id=“d” class=“FxBeanJBCZ01.Bsyso”>
    < property name=“aa” ref=“a”>< /property>
    < /bean>
    < /beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值