网站建设好吗,网站怎么做微信接口,wordpress 如何加速,个人主页展示店铺前言:上篇关于Spring的文章介绍了一些Spring的基本知识#xff0c;此篇文章主要分享一下如何配置Spring环境#xff0c;如何注入等。
Spring项目构建
导入Spring相关JAR包
dependencygroupIdorg.springframework/groupIdartifactIdspring…前言:上篇关于Spring的文章介绍了一些Spring的基本知识此篇文章主要分享一下如何配置Spring环境如何注入等。
Spring项目构建
导入Spring相关JAR包
dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.4.RELEASE/version
/dependency
准备Spring配置文件 在classpath的根目录下新建一个applicationContext.xml配置文件文件名可以自定义但是通常使用applicationContext这个名字 添加文档声明和约束这个东西不需要记忆 可以参考文档中英文文档都可以 spring-framework-4.1.2.RELEASE\docs\spring-framework-reference\pdf 可以参考资源中的资料 可以百度spring的配置文件 也可以直接拿以下内容去修改
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocationhttp://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsdbean id... class...!-- collaborators and configuration for this bean go here --/bean
/beans
编写一个类
public class MyBean {public void hello(){System.out.println(hello spring...);}
}
将编写的类交给Spring容器管理 Spring是一个容器我们需要把我们的类交给Spring去管理。 因为我们的测试是创建一个普通的类然后再通过Spring帮我们把这个类的对象创建出来就算是成功了 在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置 beans ...bean idmyBean classorg.com.Spring._01_hello.MyBean/bean
/beans 元素和属性讲解 bean元素表示对象配置或注册标签 id属性这个bean对象在Spring容器中的唯一标识也可以使用name常用id唯一特性获取这个对象的时候就可以通过这个表示来获取 class属性对应对象所属类的完全限定名。注意这里可以是JDK自带的类也可以是自己新建的类
Spring容器实例化 Spring容器对象有两种BeanFactory和ApplicationContext ApplicationContext继承自BeanFactory接口拥有更多的企业级方法推荐使用该类型
BeanFactory
BeanFactory是一个接口可以通过其实现类XmlBeanFactory获取其实例。接口中有一个getBean()方法可以获取Spring容器中配置或注册的Bean对象
Test
public void testHelloSpring() throws Exception {/***我们第一步是要启动框架而启动框架则需要拿到Spring的核心对象 *///第一步读取资源文件Resource resource new ClassPathResource(applicationContext.xml);//第二步拿到核心对象 BeanFactoryBeanFactory factory new XmlBeanFactory(resource);
}
ApplicationContext
ApplicationContext的中文意思是应用程序上下文它继承自BeanFactory接口除了包含BeanFactory的所有功能之外在国际化支持、资源访问如URL和文件、事件传播等方面进行了良好的支持被推荐为JavaEE应用之首选可应用在Java APP与Java Web中
加载工程classpath下的配置文件实例化
String conf applicationContext.xml;
ApplicationContext factory new ClassPathXmlApplicationContext(conf);
获取对象方式
方式一通过id直接拿到相应的Bean对象
//通过[xml]{.ul}中配置的id拿到对象
MyBean bean (MyBean)factory.getBean(myBean);
System.out.println(bean);
方式二通过id与对象的Class对象拿到Bean对象推荐使用
//通过id与对象的class拿到Bean对象
MyBean bean factory.getBean(myBean,MyBean.class);
System.out.println(bean);
ApplicationContext与BeanFactory的区别
联系
ApplicationContext是BeanFactory的子类拥有更多的功能与方法
区别
ApplicationContext默认是在读取配置文件的时候就会根据配置创建Bean对象迫切加载。而BeanFactory是在使用的时候才进行对象的创建懒加载/延迟加载
Spring管理bean的方式
XML
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdbean idmyBean classcom.ss._04di._01xml.MyBean/beanbean idotherBean classcom.ss._04di._01xml.OtherBean/bean
/beans
注解
配置扫描注解
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd!--扫描组件通过指定包就是找到本包或者子孙包下面加了注解Component及Controller等注解的类把它交给spring管理就相当于我们xml当中一个一个配置--context:component-scan base-packagecom.ss._04di._02annotation/context:component-scan
/beans
加注解
Component Controller Service Repository等注解后面三个是前一个子注解。其实我们任意用一个都可以但是为了代码可读性最好分门别类的使用 。
Controller就用 Controller 注解
Service就用Service注解
dao/mapper用Repository
Spring依赖注入 IoC是一种思想它的一个重点是在系统运行中动态的向某个对象提供它所需要的其他对象。这一点是通过DIDependency Injection依赖注入来实现的 Spring中的对象都是由Spring进行统一管理但是在对象中还存在属性这些属性实际上引用的也是别的对象那么这些对象也是由Spring来管理的 在实际使用时我们需要给Spring中对象的属性字段赋值这称为依赖注入DIDependency Injection 依赖注入又分为xml注入和注解注入
XML注入
Bean定义:
public class MyBean{private OtherBean otherBean;public void hello(){otherBean.hello();}public void setOtherBean(OtherBean otherbean){this.OtherBean OtherBean
}
}public class OtherBean{public void hello(){System.out.println(otherbean hello);}
}
xml定义
//xml配置
bean idotherBean classorg.com.bean.OtherBean/bean
bean idmyBean classorg.com.bean.MyBeanproperty nameotherBean refotherBean/property
/bean
测试
//测试main方法测试
//加载工程classpath下的配置文件实例化
String conf applicationContext.xml;
ApplicationContext factory new ClassPathXmlApplicationContext(conf);
注解注入
使用Autowired
Component
public class ServiceBean {Autowiredprivate DaoBean1 daoBean;public void sayHello(){System.out.println(hello spring!!!!);daoBean.hello();}
}
Component
public class DaoBean1 {public void hello(){System.out.println(hello DaoBean1!!!!!);}
}
Component
public class DaoBean {public void hello(){System.out.println(hello DaoBean!!!!!);}
}
使用Resource
public class MyBean{Resource //默认按照名字匹配【名字对了类型也必须一致】然后按照类型匹配//Resource(nameotherBean1)//指定Bean的名称private OtherBean otherBean;public void hello(){otherBean.hello();}
}public class OtherBean{public void hello(){System.out.println(otherbean hello);}
}
写在最后
Spring的依赖注入翻转控制面向切面编程是其最主要的特点与优势后续博主也会继续分享相关的文章。博主小中大厂均有面试经历每日分享JAVA全栈知识希望能与大家共同进步。