Spring最基础使用1
1. 导入Spring等jar包
2. 配置文件 applicationContext.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: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.xsd">
<!-- <context:component-scan base-package="com.chinasofti.bean"></context:component-scan> -->
<!-- 构造器方式 -->
<bean id="dog" class="day1.Dog">
<!-- 有参的类: constructor-arg 标签代表有参构造,index对应第几个参数,value是值 -->
<constructor-arg index="0" value="杀魔爷"></constructor-arg>
<constructor-arg index="1" value="888"></constructor-arg>
</bean>
<!-- 静态工厂方式 -->
<bean id="product" class="beans.staticFactorys" factory-method="getProduct"></bean>
<!-- 非公共类 -->
<bean id="div" class="beans.Div"></bean>
<!-- 普通类的方法 返回的对象 -->
<bean id="factory" class="beans.Factory" ></bean>
<bean id="product2" factory-bean="factory" factory-method="getProduct"></bean>
</beans>
3. 对应的bean 如
package day1;
public class Dog {
private String name;
int age;
public Dog(){ //无参构造
System.out.println("初始化,我是无参构造");
}
public Dog(String name){
this.name=name;
System.out.println("初始化1");
}
public Dog(String name,int age){
this.name=name;
this.age=age;
System.out.println("初始化Dog,name:"+name);
}
public void shout(){ // 方法
System.out.println("时间,不在于你拥有多少,而在于你使用多少");
}
public String getName() {
return name;
}
public void setName(String s) {
this.name =s;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
public void destory(){
System.out.println("狗不在了");
}
}
4. 获取bean
package day1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DogTest {
public static void main(String[] args) {
String path="applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(path);//实例化容器
Dog dog=(Dog) ac.getBean("dog");// 向容器ac 索取id名为 dog的bean 对象
// 容器查找bean 配置里的id 为dog 的bean 标签, 然后根据bean 标签里的class路径对应的类 来实例化对应的对象
//dog.shout();
System.out.println(dog.getName());
System.out.println(dog.getAge());
}
}
Spring最基础使用1的更多相关文章
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- spring中基础核心接口总结
spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...
- Spring Boot 基础
Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...
- Spring MVC 基础
Spring MVC 基础 1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是 ...
- Spring Boot 基础教程系列学习文档
Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...
- CgLib动态代理学习【Spring AOP基础之一】
如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...
- spring boot基础 入门
spring boot基础 spring boot 的简单搭建 spring boot 的基本用法 spring boot 基本用法 自动配置 技术集成 性能监控 源码解析 工程的构建 创建一个mav ...
- Spring的基础注解
Spring的基础注解 1.注解的概述 注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具有互换性.通常基于注解编程的程序更加简洁. (注:使用Spring注解必须导入aop包) ...
- Spring Cloud基础教程
Spring Cloud基础教程 2017-04-04 被围观 90375 次 该教程内容不定时更新,如您对这些内容感兴趣,可以关注我的博客或微信公众号! 本教程示例代码: GitHub:https ...
随机推荐
- JS 判断是否为安卓或IOS系统
其实很简单,代码如下<script type="text/javascript"> var device = navigator.userAgent; || devic ...
- VUE - 引入 npm 安装的模块 以及 uuid模块的使用
<template> <div> <form @submit.prevent="addTodo"> <in ...
- 彻底理解JavaScript中的this
this 是 JavaScript 语言的一个关键字. 它是函数运行时,在函数体内自动生成的一个对象,只能在函数体内使用. 函数的不同使用场合,this 有不同的值.总的来说,this 就是函数运行时 ...
- 029、Java中的四则运算
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 04.Delphi通过接口IInterface实现多重继承
IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 uSayHello代码如下 unit uSayHello; interface uses SysUtils, ...
- Dive into re Module in Python
Dive into RE in Python Standard re module in python is powerful to handle text manipulation,such as ...
- (五)微信小程序的跳转
我们在微信页面往往有点击一个图片就可以跳转的情况,接下来我们就学习一下这个功能 一 js版本--bindtap 实现跳转 1. 首先我们先写一个跳转的按钮(在index.wxml) <view ...
- POJ3295 Tautology重言式
Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
- Golang的进制转换实战案例
Golang的进制转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用进制概述 1>.进制概述 进制也就是进位制,是人们规定的一种进位方法.举个例子:二进制就 ...
- c#查看本机网络端口和对应的程序名
360安全卫士里面有个组件叫流量防火墙,感觉挺好用,但是不想安装360全家桶,于是自己捣鼓着用C#写一个比较简化的版本. 查看电脑上开启的TCP或UDP端口,可以用netstat命令,netstat用 ...