Spring Learning uses tags to tag resources (@Component, @Repository, @Service, and @controller) and how to use them (including how to use them in JSPs)

Source: Internet
Author: User

The first thing to do is to add the underlined part to the XML file, and the package to be scanned when the container is initialized.

Attention:

A. The scanned package section (underlined section) must be added, by default it will not scan all packages. Each package is separated by ', '. If you have the same parent package, then we can use the parent package instead. Below the underlined part, we can use COM.BJSXT to replace.

<?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-2.5.xsd           Http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd "><context:annotation-config/ ><!--Automatic assembly Be sure to add this fragment--<context:component-scan base-package= "Com.bjsxt.dao.impl,com.bjsxt.service, Com.bjsxt.model "></context:component-scan> </beans>


B. In version 2.5 ( @Component , @Repository , @Service and @Controller ) Four tags represent the same meaning and may differ in future versions.

c.    When labeling resources such as components, try to add names such as@Component("stuDaoImpl")。默认的名称是 类名首字母小写。

<pre name= "code" class= "java" >package com.bjsxt.dao.impl;import Javax.annotation.resource;import Org.springframework.stereotype.component;import com.bjsxt.dao.*;import com.bjsxt.model.Student; @Component (" Studaoimpl ") public class Studentdaoimpl implements studentdao{@Overridepublic void Studentsave (Student s) {          SYSTEM.OUT.PRINTLN ("Student is saved!") ");}}


d.      使用方法 在set方法或者属性名前加入 @resource(name="stuDaoImpl"),推荐加入名称,默认是按照类型进行查找。例如:

Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.stereotype.component;import Com.bjsxt.dao.*;import com.bjsxt.dao.impl.*;import com.bjsxt.model.*; @Component ("Studentservice")//declaring resource public Class Studentservice {private Studentdao studentdao;public Studentdao Getstudentdao () {return studentdao;} @Resource (name= "Studaoimpl")//Inject public void Setstudentdao (Studentdao studentdao) {This.studentdao = Studentdao;} public void Add (Student s) {this.studentDao.StudentSave (s);}

E.test mode is still the same as the previous injection method, note the

Studentstudent=(Student)ctx.getBean("student");是我们用标签在student类中声明的@Component("student")。Spring容器会能通过ctx(相当于beanFactory)找到。

Package Com.bjsxt.service;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bjsxt.model.student;public Class studentservicetest {@Testpublic void Test () {System.out.println ("before program runs ...."); ApplicationContext ctx=new classpathxmlapplicationcontext ("Beans.xml"); SYSTEM.OUT.PRINTLN ("program run starts ...."); Studentservice sservice= (Studentservice) Ctx.getbean ("Studentservice"); Student student= (Student) Ctx.getbean ("Student"); Student student2= (Student) Ctx.getbean ("Student"); System.out.println (student2==student); Sservice.add (student);}}

f.   在jsp页面需要处理业务,有java代码需要spring注入service。

Need to tag the class you want to get now

@Component ("docrelationservice") First JSP page Import

<% @page import= "Org.springframework.context.ApplicationContext"%>
<% @pageimport = "Org.springframework.web.context.support.WebApplicationContextUtils"%>

Get Spring injected

<%
ApplicationContext Context =webapplicationcontextutils
. Getwebapplicationcontext (application);

Docrelationservicedocrelationservice = (docrelationservice) context
. Getbean ("Docrelationservice");

%>

g.   假如类A由spring容器管理,在类B中引用A,如果想在B中的A是由spring容器创建的,有两种方法:

     a).类B也由spring容器管理(即类B前有@compoment(“b”)标签),并注入A,用BeanFactory.getBean("b")得到B实例,即可(推荐).

     b).类B不由spring容器管理,在类B中用代码 (A)BeanFactory.getBean("a")得到A实例,即可.(不推荐,会产生两个beanFactory因为在类B中需要用BeanFactory,所以我们必须在B中new一个)

c)类B创建实例时候如果采用a)方法,决不能采用B b=new B();的方式,否则会报nullpoint异常。

d)ClassPathXmlApplicationContext建立在beanFactory基础之上,很少有人直接使用。

Test code:

Package Com.bjsxt.service;import Org.junit.test;import Org.springframework.beans.factory.beanfactory;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.bjsxt.model.student;public Class studentservicetest {@Testpublic void Test () {System.out.println ("before program runs ....");//beanfactory ctx=new Classpathxmlapplicationcontext ("Beans.xml");//classpathxmlapplicationcontext is based on beanfactory and few people use it directly.    applicationcontext ctx=new classpathxmlapplicationcontext ("Beans.xml");//Get the Spring container System.out.println directly (" Program run start .... "); Studentservice sservice= (Studentservice) Ctx.getbean ("Studentservice");//studentservice equals class B//studentservice Sservice=new Studentservice (); Student student= (Student) Ctx.getbean ("Student"); Student student2= (Student) Ctx.getbean ("Student"); System.out.println (student2==student); Sservice.add (student);}}




Spring Learning uses tags to tag resources (@Component, @Repository, @Service, and @controller) and how to use them (including how to use them in JSPs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.