spring框架应用的是ioc模式,ioc模式是指控制反转模式,本质是你不去创建对象让spring框架给你创建对象你去使用对象。多种开发模式通过配置文件和注解的方式去开发的都很值得去学习
构造一个接口
public interface userdo {
void select();
}
package spring;
public class mssqldaodimpl implements userdo{
@Override
public void select() {
System.out.println("mssqlselect");
}
}
public class mysqldaoimpl implements userdo{
@Override
public void select() {
System.out.println("mysqldaoimp");
}
}
如果按照平时开发的思路这个时候会去写一个test类去创建mssqldaodimpl、mysqldaoimpl去构建对象然后去执行两个的select方法
如果是在spring里面就会用ioc这种方式,这两个类都是ioc容器里面的两个bean只需要通过spring的模式去访问他们
先写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqldaoimpl" class="spring.mysqldaoimpl"/>
<bean id="mssqldaoimpl" class="spring.mssqldaodimpl"/>
</beans>
id是你用来寻找bean的名字,后面是更上的类
测试类
package spring;
import spring.userdo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
userdo mysqldoimpl = (userdo) context.getBean("mssqldaoimpl");
userdo mysqldaoimpl = (userdo) context.getBean("mysqldaoimpl");
mysqldaoimpl.select();
mysqldoimpl.select();
}
}
bean对象是被ioc容器所创造的,bean对象里面的属性也可以由ioc容器来构造
构造注入:顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置
的方式,让 spring 框架来为我们注入参数
先构造一个实体类
package spring;
import java.util.Date;
public class People {
private String name;
private Integer age;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
private Date date;
public People(){
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
", date=" + date +
'}';
}
public People(String name, Integer age, Date date) {
this.name = name;
this.age = age;
this.date=date;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqldaoimpl" class="spring.mysqldaoimpl"/>
<bean id="mssqldaoimpl" class="spring.mssqldaodimpl"/>
<bean id="People" class="spring.People">
<constructor-arg name="name" value="xiaohu"/>
<constructor-arg name="age" value="12"/>
<constructor-arg name="date" ref="now"/>
</bean>
<bean id="People2" class="spring.People">
<property name="date" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"/>
</beans>
<bean id="People" class="spring.People">
<constructor-arg name="name" value="xiaohu"/>//值可以直接通过value来赋值
<constructor-arg name="age" value="12"/>
<constructor-arg name="date" ref="now"/>//如果值是一个类可以用ref加一个新的bean对象来完成
</bean>
<bean id="now" class="java.util.Date"/>
name:找的是类中 set 方法后面的部分
ref:给属性赋值是其他 bean 类型的
value:给属性赋值是基本数据类型和 string 类型的
实际开发中,此种方式用的较多。
Bean标签的作用
作用:
用于配置对象让 spring 来创建的。
默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
属性:
id: 给对象在容器中提供一个唯一标识。用于获取对象。
class: 指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
value: 用constructor-arg指定实例对象的具体属性。
*scope: 指定对象的作用范围。
* singleton :默认值,单例的.
* prototype :多例的.
* request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
* session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
* global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么
*globalSession 相当于 session.
*init-method: 指定类中的初始化方法名称。
*destroy-method: 指定类中销毁方法名称。
constructor-arg标签
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
在spring框架里面可以自动装配Bean。我们只需要在bean标签里面加上 autowire就可以了。
autowire属性:
先看源码:
per类
package spring.auto_bean;
public class per {
private String name;
private Dog dog;
private Cat cat;
public per(String name, Dog dog, Cat cat) {
this.name = name;
this.dog = dog;
this.cat = cat;
}
public per() {
}
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 Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "per{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
dog和cat类
package spring.auto_bean;
public class Dog {
public void method(){
System.out.println("dog");
}
}
看一下怎么配置自动装配的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="per" class="spring.auto_bean.per" autowire="byType">
<property name="name" value="xiaoming"/>
</bean>
<bean id="cat" class="spring.auto_bean.Cat"/>
<bean id="dog" class="spring.auto_bean.Dog"/>
</beans>
看一下标签内容
no :缺省情况下,自动配置是通过“ref”属性手动设定
byName:根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
byType:按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
constructor:在构造函数参数的byType方式。
autodetect:如果找到默认的构造函数,使用"自动装配用构造"否则,使用“按类型自动装配
简单理解一下这个标签如果我此时配置文件是,我选用的是通过名字自动装配,我在per类里面写的是小写的cat
在这里的bean id写的是大写的,他输出的结果是:per{name='xiaoming', dog=spring.auto_bean.Dog@4a94ee4, cat=null},cat为空没有装配到
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="per" class="spring.auto_bean.per" autowire="byName">
<property name="name" value="xiaoming"/>
</bean>
<bean id="Cat" class="spring.auto_bean.Cat"/>
<bean id="dog" class="spring.auto_bean.Dog"/>
</beans>
如果我使用byType结果是:per{name='xiaoming', dog=spring.auto_bean.Dog@6d763516, cat=spring.auto_bean.Cat@52bf72b5}
Cat被识别到因为用的是class去识别的
直接通过配置文件的方式完成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 还是采用set注入-->
<bean id="arrlist" class="spring.auto_bean.per">
<property name="cat">
<array>
<value>100</value>
<value>100</value>
</array>
</property>
</bean>
<bean id="map" class="spring.auto_bean.per">
<property name="name">
<map><entry key="name" value="HELLO"></entry> </map>
</property>
</bean>
</beans>
各种数据类型对应不同的格式,只需要简单的了解一下就OK
如果使用注解自动装载bean的话,我们需要对xml进行一个配置,加上context:annotation-config标签,并且需要导入约束。
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="spring.auto_bean.Cat"/>
<bean id="dog" class="spring.auto_bean.Cat"/>
<bean id="person" class="spring.auto_bean.per">
<property name="name" value="xiaoming"/>
</bean>
</beans>
然后在类上面写上注解标签@Autowired
容器获取方式1:通过了路径活得xml配置文件
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean2.xml");
配置方式2:通过文件路径获取配置文件
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("D://bean2.xml");
BeanFactory是IOC顶层的接口,初始化BeanFactory对象时,加载bean延迟加载
AppLicationContext接口是Spring容器的核心接口,初始化Bean的时候立即加载
ApplicationContext常用的初始化分类
常用的类:ClassPathXmlApplicationContext
FileSystemApplicationContext
先写一个主类
package spring.editbean;
import org.springframework.stereotype.Component;
@Component("Animal")
public class Anmial {
private chicken chicken;
private duke duke;
public spring.editbean.chicken getChicken() {
return chicken;
}
public void setChicken(spring.editbean.chicken chicken) {
this.chicken = chicken;
}
public spring.editbean.duke getDuke() {
return duke;
}
public void setDuke(spring.editbean.duke duke) {
this.duke = duke;
}
public Anmial(spring.editbean.chicken chicken, spring.editbean.duke duke) {
this.chicken = chicken;
this.duke = duke;
}
@Override
public String toString() {
return "Anmial{" +
"chicken=" + chicken +
", duke=" + duke +
'}';
}
}
和两个分类
package spring.editbean;
import org.springframework.stereotype.Component;
@Component("duke")
public class duke {
public void method(){
System.out.println("duke被执行了");
}
public duke() {
System.out.println("duke的构造函数被执行了");
}
}
在你需要自定义的类上面加上@Component("duke"):意思就是定义一个组件然后给他的名字是,然后在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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="spring"/>
</beans>
<context:component-scan base-package="spring"/>去spring包下搜索组件。
步骤:
在类上面创建注释
如果类有其他引用数据类型需要在其他引用数据类型上面也要加注释
在xml文件中配置自动搜索和搜索路径
测试代码
public class TEST {
public static void main(String[] args) {
ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean5.xml");
Anmial anmial = (Anmial) classPathXmlApplicationContext.getBean("Animal");
System.out.println(anmial);
}
}
输出结果:Anmial{chicken=spring.editbean.chicken@62230c58, duke=spring.editbean.duke@2cd2a21f}+两个分类的构造函数结果。
bean注解配置的三个分支
@Controller 用于表现层的注解bean
@Service 用于业务层bean注解
@Repository 用于数据层的bean定义
@Scope定义作用范围
@PostConstruct构造方法前执行
@PreDestroy销毁方法前执行
把配置文件改成配置类
@Configuration
@ComponentScan("spring")
@PropertySource("value.properties")
public class springconfig {
}
@Configuration确定为配置类
@ComponentScan("去扫描包,找到bean")
@PropertySource("value.properties")对value的配置文件的的配置用键值对的方式存在。
自动配置
@Component("Animal")
public class Anmial {
@Autowired
private chicken chicken;
@Autowired
private duke duke;
@Value("${name}")
private String name;
用到的方法是用写配置类的方法,然后通过Bean去构造一个bean
@Configuration
//定义一个方法去管理对象
public class mangerbean {
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Drvier");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/tese");
druidDataSource.setUsername("root");
druidDataSource.setPassword("zhonglin");
return druidDataSource;
}
}
测试方法
@Test
public void manger(){
ApplicationContext acac = new AnnotationConfigApplicationContext(mangerbean.class);
DataSource bean = acac.getBean(DataSource.class);
System.out.println(bean);
}
}
ioc模型很重要这种bean注入的形式不去自己创造对象的思路很重要,各种重要的标签可以做完为以后形成构造链分析的好帮手,同时现在也正式开始恢复正常的学习
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,
Ⅰ软件测试基础一、软件测试基础理论1、软件测试的必要性所有的产品或者服务上线都需要测试2、测试的发展过程3、什么是软件测试找bug,发现缺陷4、测试的定义使用人工或自动的手段来运行或者测试某个系统的过程。目的在于检测它是否满足规定的需求。弄清预期结果和实际结果的差别。5、测试的目的以最小的人力、物力和时间找出软件中潜在的错误和缺陷6、测试的原则28原则:20%的主要功能要重点测(eg:支付宝的支付功能,其他功能都是次要的)80%的错误存在于20%的代码中7、测试标准8、测试的基本要求功能测试性能测试安全性测试兼容性测试易用性测试外观界面测试可靠性测试二、质量模型衡量一个优秀软件的维度①功能性功
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg