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

有做任务赚赏金的网站吗seo关键词优化服务

有做任务赚赏金的网站吗,seo关键词优化服务,营销型平台网站建设,做红k线网站前言:上篇关于Spring的文章介绍了一些Spring的基本知识&#xff0c;此篇文章主要分享一下如何配置Spring环境&#xff0c;如何注入等。 Spring项目构建 导入Spring相关JAR包 <dependency><groupId>org.springframework</groupId><artifactId>spring…

前言:上篇关于Spring的文章介绍了一些Spring的基本知识,此篇文章主要分享一下如何配置Spring环境,如何注入等。

Spring项目构建

导入Spring相关JAR包

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.4.RELEASE</version>
</dependency>

准备Spring配置文件

  1. 在classpath的根目录下新建一个applicationContext.xml配置文件,文件名可以自定义,但是通常使用applicationContext这个名字;

  2. 添加文档声明和约束(这个东西不需要记忆):

    1. 可以参考文档,中英文文档都可以;

      1. spring-framework-4.1.2.RELEASE\docs\spring-framework-reference\pdf

    2. 可以参考资源中的资料;

    3. 可以百度spring的配置文件;

    4. 也可以直接拿以下内容去修改

<?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="..." class="..."><!-- collaborators and configuration for this bean go here --></bean>
</beans>

编写一个类

public class MyBean {public void hello(){System.out.println("hello spring...");}
}

将编写的类交给Spring容器管理

  1. Spring是一个容器,我们需要把我们的类交给Spring去管理。 因为,我们的测试是创建一个普通的类,然后再通过Spring帮我们把这个类的对象创建出来就算是成功了;

  2. 在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置

    <beans ...><bean id="myBean" class="org.com.Spring._01_hello.MyBean"></bean>
    </beans>
  3. 元素和属性讲解:

    bean元素:表示对象配置或注册标签;

    id属性:这个bean对象在Spring容器中的唯一标识,也可以使用name,常用id(唯一特性),获取这个对象的时候就可以通过这个表示来获取;

    class属性:对应对象所属类的完全限定名。注意这里可以是JDK自带的类,也可以是自己新建的类;

Spring容器实例化

  1. Spring容器对象有两种:BeanFactory和ApplicationContext;

  2. 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 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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"
>    <bean id="myBean" class="com.ss._04di._01xml.MyBean"></bean><bean id="otherBean" class="com.ss._04di._01xml.OtherBean"></bean>
</beans>

注解

配置扫描注解

<?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/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-package="com.ss._04di._02annotation"></context:component-scan>
</beans>

加注解

@Component @Controller @ Service @Repository等注解,后面三个是前一个子注解。其实我们任意用一个都可以,但是为了代码可读性,最好分门别类的使用 。

Controller就用 @Controller 注解

Service就用@Service注解

dao/mapper用@Repository

Spring依赖注入

  1. IoC是一种思想,它的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的;

  2. Spring中的对象都是由Spring进行统一管理,但是在对象中还存在属性,这些属性实际上引用的也是别的对象,那么这些对象也是由Spring来管理的;

  3. 在实际使用时,我们需要给Spring中对象的属性字段赋值,这称为依赖注入DI(Dependency Injection);

  4. 依赖注入又分为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 id="otherBean" class="org.com.bean.OtherBean"></bean>
<bean id="myBean" class="org.com.bean.MyBean"><property name="otherBean" ref="otherBean"></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(name="otherBean1")//指定Bean的名称private OtherBean otherBean;public void hello(){otherBean.hello();}
}public class OtherBean{public void hello(){System.out.println("otherbean hello");}
}

写在最后

Spring的依赖注入,翻转控制,面向切面编程是其最主要的特点与优势,后续博主也会继续分享相关的文章。博主小,中,大厂均有面试经历,每日分享JAVA全栈知识,希望能与大家共同进步。

http://www.dinnco.com/news/53539.html

相关文章:

  • 平潭城乡住房建设厅网站适合发表个人文章的平台
  • 免费1级做爰片在线观看 历史网站百度seo培训要多少钱
  • 做网站如何引用头部资源搜索神器
  • 摄影网站采用照片做宣传 版权费是多少广州网站优化
  • 如何做招聘网站分析广州seo外包多少钱
  • 做网站的叫什么软件黑马it培训班出来现状
  • 福田公司seo外包公司
  • 美国人在床上做裸身体网站啊长沙百度网站推广公司
  • 网站统计代码放哪里梅花seo 快速排名软件
  • 做平面设计兼职的网站博客网站登录
  • 本溪北京网站建设内部搜索引擎优化
  • 湖北建设厅网站查询长春网站建设方案托管
  • wordpress默认密码徐州网页关键词优化
  • 网络营销策划的定义站长工具seo诊断
  • 南京做网站哪家公司好爱战网官网
  • 青岛网站建设莫道网络成都高新seo
  • 多种成都网站建设阳山网站seo
  • 河南郑州建设网站seo关键词排名优化软件怎么选
  • 如何搭建钓鱼网站网络推广靠谱吗
  • 佛山网站建设怎样做长沙营销推广
  • 网站怎么做能快速有排名电商具体是做什么的
  • 做外贸soho网站的公司吗培训机构管理系统哪个好
  • 河北网站建设及推广免费搭建网站平台
  • 企业网站系统有哪些如何推广一款app
  • 如何用外网ip做网站网络推广方法有哪些
  • 做一元购网站seo 关键词优化
  • 政协网站建设怎么注册个人网站
  • 店铺外卖网站怎么做google play官网下载
  • 网站转出营口seo
  • 网站设计结构广东公共广告20120708