当前位置: 首页 > news >正文

查找邮箱注册过的网站dede 网站名称不显示

查找邮箱注册过的网站,dede 网站名称不显示,北京装饰公司十大排名榜,seo课程排行榜目录 Spring简介Spring体系结构SpringIOC控制反转思想自定义对象容器Spring实现IOCSpring容器类型容器接口容器实现类对象的创建方式使用构造方法使用工厂类的方法使用工厂类的静态方法对象的创建策略对象的销毁时机生命周期方法获取Bean对象的方式通过id/name获取通过类型获取… 目录 Spring简介Spring体系结构SpringIOC控制反转思想自定义对象容器Spring实现IOCSpring容器类型容器接口容器实现类对象的创建方式使用构造方法使用工厂类的方法使用工厂类的静态方法对象的创建策略对象的销毁时机生命周期方法获取Bean对象的方式通过id/name获取通过类型获取通过类型id/name获取 Spring简介 Spring是一个开源框架为简化企业级开发而生。它以IOC控制反转和AOP面向切面为思想内核提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术并可以整合众多第三方框架。 Spring将很多复杂的代码变得优雅简洁有效的降低代码的耦合度极大的方便项目的后期维护、升级和扩展。 Spring官网地址https://spring.io/ Spring体系结构 Spring框架根据不同的功能被划分成了多个模块这些模块可以满足一切企业级应用开发的需求在开发过程中可以根据需求有选择性地使用所需要的模块。 Core ContainerSpring核心模块任何功能的使用都离不开该模块是其他模块建立的基础。Data Access/Integration该模块提供了数据持久化的相应功能。Web该模块提供了web开发的相应功能。AOP提供了面向切面编程实现Aspects提供与AspectJ框架的集成该框架是一个面向切面编程框架。Instrumentation提供了类工具的支持和类加载器的实现可以在特定的应用服务器中使用。Messaging为Spring框架集成一些基础的报文传送应用Test提供与测试框架的集成 SpringIOC 控制反转思想 IOC(Inversion of Control) 程序将创建对象的权利交给框架。 之前在开发过程中对象实例的创建是由调用者管理的代码如下 public interface StudentDao {// 根据id查询学生Student findById(int id); }public class StudentDaoImpl implements StudentDao{Overridepublic Student findById(int id) {// 模拟从数据库查找出学生return new Student(1,张三,北京);} }public class StudentService {public Student findStudentById(int id){// 此处就是调用者在创建对象StudentDao studentDao new StudentDaoImpl();return studentDao.findById(1);} }这种写法有两个缺点 浪费资源StudentService调用方法时即会创建一个对象如果不断调用方法则会创建大量StudentDao对象。代码耦合度高假设随着开发我们创建了StudentDao另一个更加完善的实现类StudentDaoImpl2如果在StudentService中想使用StudentDaoImpl2则必须修改源码。 而IOC思想是将创建对象的权利交给框架框架会帮助我们创建对象分配对象的使用控制权由程序代码转移到了框架中控制权发生了反转这就是Spring的IOC思想。而IOC思想可以完美的解决以上两个问题。 自定义对象容器 接下来我们通过一段代码模拟IOC思想。创建一个集合容器先将对象创建出来放到容器中需要使用对象时只需要从容器中获取对象即可而不需要重新创建此时容器就是对象的管理者。 创建实体类 public class Student {private int id;private String name;private String address;// 省略getter/setter/构造方法/tostring }创建Dao接口和实现类 public interface StudentDao {// 根据id查询学生Student findById(int id); }public class StudentDaoImpl implements StudentDao{Overridepublic Student findById(int id) {// 模拟从数据库查找出学生return new Student(1,张三,北京);} }public class StudentDaoImpl2 implements StudentDao{Overridepublic Student findById(int id) {// 模拟根据id查询学生System.out.println(新方法);return new Student(1,张三,北京);} }创建配置文件bean.properties该文件中定义管理的对象 studentDaocom.Spring.dao.StudentDaoImpl创建容器管理类该类在类加载时读取配置文件将配置文件中配置的对象全部创建并放入容器中。 public class Container {static MapString,Object map new HashMap();static {// 读取配置文件InputStream is Container.class.getClassLoader().getResourceAsStream(bean.properties);Properties properties new Properties();try {properties.load(is);} catch (IOException e) {e.printStackTrace();}// 遍历配置文件的所有配置EnumerationObject keys properties.keys();while (keys.hasMoreElements()){String key keys.nextElement().toString();String value properties.getProperty(key);try {// 创建对象Object o Class.forName(value).newInstance();// 将对象放入集合中map.put(key,o);} catch (Exception e) {e.printStackTrace();}}}// 从容器中获取对象public static Object getBean(String key){return map.get(key);} }创建Dao对象的调用者StudentService public class StudentService {public Student findStudentById(int id){// 从容器中获取对象StudentDao studentDao (StudentDao) Container.getBean(studentDao);System.out.println(studentDao.hashCode());return studentDao.findById(id);} }测试StudentService public class Test {public static void main(String[] args) {StudentService studentService new StudentService();System.out.println(studentService.findStudentById(1));System.out.println(studentService.findStudentById(1));} }测试结果 StudentService从容器中每次拿到的都是同一个StudentDao对象节约了资源。 如果想使用StudentDaoImpl2对象只需要修改bean.properties的内容为 studentDaocom.Spring.dao.StudentDaoImpl2即可无需修改Java源码。 Spring实现IOC 接下来我们使用Spring实现IOCSpring内部也有一个容器用来管理对象。 创建Maven工程引入依赖 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.13/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency /dependencies创建POJO类、Dao类和接口 public class Student {private int id;private String name;private String address;// 省略getter/setter/构造方法/tostring }public interface StudentDao {// 根据id查询学生Student findById(int id); }public class StudentDaoImpl implements StudentDao{Overridepublic Student findById(int id) {// 模拟从数据库查找出学生return new Student(1,张三,北京);} }编写xml配置文件配置文件中配置需要Spring帮我们创建的对象。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdbean idstudentDao classcom.Spring.dao.StudentDaoImpl/bean/beans测试从Spring容器中获取对象。 public class TestContainer {Testpublic void t1(){// 创建Spring容器ApplicationContext ac new ClassPathXmlApplicationContext(bean.xml);// 从容器获取对象StudentDao studentDao1 (StudentDao) ac.getBean(studentDao);StudentDao studentDao2 (StudentDao) ac.getBean(studentDao);System.out.println(studentDao1.hashCode());System.out.println(studentDao2.hashCode());System.out.println(studentDao1.findById(1));} }Spring容器类型 容器接口 BeanFactoryBeanFactory是Spring容器中的顶层接口它可以对Bean对象进行管理。 ApplicationContextApplicationContext是BeanFactory的子接口。它除了继承 BeanFactory的所有功能外还添加了对国际化、资源访问、事件传播等方面的良好支持。 ApplicationContext有以下三个常用实现类 容器实现类 ClassPathXmlApplicationContext该类可以从项目中读取配置文件FileSystemXmlApplicationContext该类从磁盘中读取配置文件AnnotationConfigApplicationContext使用该类不读取配置文件而是会读取注解 Test public void t2(){// 创建spring容器// ApplicationContext ac new ClassPathXmlApplicationContext(bean.xml);ApplicationContext ac new FileSystemXmlApplicationContext(C:\\Users\\a\\IdeaProjects\\spring_demo\\src\\main\\resources\\bean.xml);// 从容器中获取对象StudentDao userDao (StudentDao) ac.getBean(studentDao);System.out.println(userDao);System.out.println(userDao.findById(1)); }对象的创建方式 Spring会帮助我们创建bean那么它底层是调用什么方法进行创建的呢 使用构造方法 Spring默认使用类的空参构造方法创建bean // 假如类没有空参构造方法将无法完成bean的创建 public class StudentDaoImpl implements StudentDao{public StudentDaoImpl(int a){}Overridepublic Student findById(int id) {// 模拟根据id查询学生return new Student(1,张三,北京);} }使用工厂类的方法 Spring可以调用工厂类的方法创建bean 创建工厂类工厂类提供创建对象的方法 public class StudentDaoFactory {public StudentDao getStudentDao(){return new StudentDaoImpl(1);} }在配置文件中配置创建bean的方式为工厂方式。 !-- id工厂对象的idclass工厂类 -- bean idstudentDaoFactory classcom.Spring.dao.StudentDaoFactory/bean !-- idbean对象的idfactory-bean工厂对象的idfactory-method工厂方法 -- bean idstudentDao factory-beanstudentDaoFactory factory-methodgetStudentDao/bean测试 使用工厂类的静态方法 Spring可以调用工厂类的静态方法创建bean 创建工厂类工厂提供创建对象的静态方法。 public class StudentDaoFactory2 {public static StudentDao getStudentDao2() {return new StudentDaoImpl();} }在配置文件中配置创建bean的方式为工厂静态方法。 !-- id:bean的id class:工厂全类名 factory-method:工厂静态方法 -- bean idstudentDao classcom.Spring.dao.StudentDaoFactory2 factory-methodgetStudentDao2/bean测试 对象的创建策略 Spring通过配置bean中的scope属性设置对象的创建策略共有五种创建策略 singleton单例默认策略。整个项目只会创建一个对象通过bean中的lazy-init属性可以设置单例对象的创建时机 lazy-initfalse(默认)立即创建在容器启动时会创建配置文件中的所有Bean对象。 lazy-inittrue延迟创建第一次使用Bean对象时才会创建。 配置单例策略 !-- bean idstudentDao classcom.Spring.dao.StudentDaoImpl2 scopesingleton lazy-inittrue/bean-- bean idstudentDao classcom.Spring.dao.StudentDaoImpl2 scopesingleton lazy-initfalse /bean测试单例策略 // 为Bean对象的类添加构造方法 public StudentDaoImpl2(){System.out.println(创建StudentDao); } Test public void t2(){// 创建Spring容器ApplicationContext ac new ClassPathXmlApplicationContext(bean1.xml);// 从容器获取对象StudentDao studentDao1 (StudentDao) ac.getBean(studentDao);StudentDao studentDao2 (StudentDao) ac.getBean(studentDao);StudentDao studentDao3 (StudentDao) ac.getBean(studentDao);System.out.println(studentDao1.hashCode());System.out.println(studentDao2.hashCode());System.out.println(studentDao3.hashCode()); }prototype多例每次从容器中获取时都会创建对象。 !-- 配置多例策略 -- bean idstudentDao classcom.Spring.dao.StudentDaoImpl2 scopeprototype/beanrequest每次请求创建一个对象只在web环境有效。 session每次会话创建一个对象只在web环境有效。 gloabal-session一次集群环境的会话创建一个对象只在web环境有效。 对象的销毁时机 对象的创建策略不同销毁时机也不同 singleton对象随着容器的销毁而销毁。prototype使用JAVA垃圾回收机制销毁对象。request当处理请求结束bean实例将被销毁。session当HTTP Session最终被废弃的时候bean也会被销毁掉。gloabal-session集群环境下的session销毁bean实例也将被销毁。 生命周期方法 Bean对象的生命周期包含创建——使用——销毁Spring可以配置Bean对象在创建和销毁时自动执行的方法 定义生命周期方法 public class StudentDaoImpl2 implements StudentDao{// 创建时自动执行的方法public void init(){System.out.println(创建StudentDao);}// 销毁时自动执行的方法public void destory(){System.out.println(销毁StudentDao);} }配置生命周期方法 !-- init-method:创建对象时执行的方法 destroy-method:销毁对象时执行的方法 -- bean idstudentDao classcom.Spring.dao.StudentDaoImpl2 scopesingletoninit-methodinit destroy-methoddestory/bean测试 Test public void t3(){// 创建Spring容器ClassPathXmlApplicationContext ac new ClassPathXmlApplicationContext(bean1.xml);// 销毁Spring容器ClassPathXmlApplicationContext才有销毁容器的方法ac.close(); }获取Bean对象的方式 Spring有多种获取容器中对象的方式 通过id/name获取 配置文件 bean namestudentDao classcom.Spring.dao.StudentDaoImpl2/bean|| bean idstudentDao classcom.Spring.dao.StudentDaoImpl2/bean获取对象 StudentDao studentDao (StudentDao) ac.getBean(studentDao);通过类型获取 配置文件 bean namestudentDao classcom.Spring.dao.StudentDaoImpl2/bean获取对象 StudentDao studentDao2 ac.getBean(StudentDao.class);可以看到使用类型获取不需要强转。 通过类型id/name获取 虽然使用类型获取不需要强转但如果在容器中有一个接口的多个实现类对象则获取时会报错此时需要使用类型id/name获取 配置文件 bean namestudentDao classcom.Spring.dao.StudentDaoImpl2/bean bean namestudentDao1 classcom.Spring.dao.StudentDaoImpl/bean获取对象 StudentDao studentDao2 ac.getBean(studentDao,StudentDao.class);
http://www.pierceye.com/news/368440/

相关文章:

  • 建筑网站推荐wordpress hook api
  • 昆明做网站公司哪家好安卓优化
  • 魔站建站系统哪家好国内知名的包装设计公司
  • 福田区住房和建设局网站早晨设计 做网站设计吗
  • 郑州轨道网站开发手机怎么做动漫微电影网站
  • vscode网站开发昆明做网站找启搜网络
  • 如何评估网站虚拟商品交易网站建设
  • 太原网站优化教程pycharm做网站
  • 哪些网站做英语比较好免费下载模板ppt
  • 网站建设运营计划书wordpress 维护页面
  • 襄阳定制型网站开发前端网页设计招聘
  • 网站备案报价深圳市住房和建设局官网首页
  • 宁波江北区网站推广联系方式做一个论坛网站要多少钱
  • 网站制作无锡台州建设工程网站
  • 云网站 制作如何做一个网页
  • 微信免费建站新建网站站点的
  • 云网站制作的流程世界500强企业排名
  • 巨久科技网站建设做出个人网站什么水平
  • 做外贸网站怎么做做网站3个月
  • 县局网站建设招标网站建设人文类
  • 网站开发亿玛酷给力5上海logo在线制作
  • 网站重新备案搞个网站需要多少钱
  • 海南微信网站制作平台网络计划的优化
  • 域名的正确书写格式自动seo优化
  • 怎样在网站做友情链接网页什么设计
  • 做seo网站营销推广南宁建设职业技术学院招聘信息网站
  • 网站建设全网推广小程序手机网站怎么优化
  • wordpress 网站logowin系统没有wordpress
  • 玉山电商网站建设东莞市建设规划局网站
  • 网站建设运营公司企业特色c2c的代表性的电商平台