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

有做任务赚赏金的网站吗项目推广渠道有哪些

有做任务赚赏金的网站吗,项目推广渠道有哪些,做网站需要考虑什么,做挂广告网站前言:上篇关于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.zhongyajixie.com/news/4421.html

相关文章:

  • 服务器建设网站湖南企业seo优化
  • 长沙企业网站seo关键词优化工具互点
  • 编程项目实例网站国际形势最新消息
  • 建设注册中心网站软文营销的宗旨是什么
  • 服务器做视频网站吗安卓优化大师手机版
  • 外贸网站该怎么做淘宝关键词搜索排名
  • 重庆新闻联播回看seo收索引擎优化
  • 微商城网站建设信息制作网页的流程步骤
  • 北京社交网站建设seo网络推广师招聘
  • 电子商务网站规划与...百度知道灰色词代发收录
  • 郑州网站设计的公司成品网站1688入口网页版怎样
  • 怎么用 c文件做网站全网营销系统怎么样
  • 开发app和网站建设那个好些互联网营销平台有哪些
  • 网站建设表单基本操作网络营销服务
  • pytson做网站安全吗领硕网站seo优化
  • 徐州最好网站建设seo教程下载
  • 无锡中小企业网站制作高端网站定制公司
  • 利用高权重网站做关键词信息流广告投放
  • 哪个网站做ppt模板赚钱新闻发布会稿件
  • 阿里云的wordpress建站竞价推广招聘
  • 如何使用ftp上传网站怎样有效的做网上宣传
  • 新网站建设的流程什么时候友情链接
  • 做企业网站有什么工作内容深圳seo推广外包
  • 网站制作公司承担人民日报新闻消息
  • 广西上林县住房城乡建设网站360网站收录提交
  • 国内好看的网站设计百度客服中心
  • 手机上那个网站做农产品推广比较好百度下载安装到手机
  • 网站开发中遇到的主要问题谷歌推广开户
  • 网站建设预算计算方法网络营销推广网站
  • 做网站要会没软件百度关键词seo公司