长春做网站网站,百度seo最新算法,腾讯云做网站怎么样,数据分析对网站建设的重要性首先#xff0c;在xml其中新增部分标有下划线的文件#xff0c;容器初始化的时候需要扫描包 注意#xff1a; a. 包款扫描(下划线部分)一定要加#xff0c;默认是不扫描整个包。与每一包之间’#xff0c;’开。如过具有同样的父包#xff0c;那么我们能够用父包来取… 首先在xml其中新增部分标有下划线的文件容器初始化的时候需要扫描包 注意 a. 包款扫描(下划线部分)一定要加默认是不扫描整个包。与每一包之间’’开。如过具有同样的父包那么我们能够用父包来取代。例如以下划线部分我们能够用com.bjsxt来取代。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://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 /!-- 自己主动装配一定要 加上此片段-- context:component-scan base-packagecom.bjsxt.dao.impl,com.bjsxt.service,com.bjsxt.model/context:component-scan /beans b. 在2.5版本号中(Component、Repository、Service和Controller)四个标签代表同样的含义以后的版本号中可能会有差别。 c. 在标注组件等资源时候尽量加上名称比如Component(stuDaoImpl)。默认的名称是 类名首字母小写。 pre namecode classjavapackage 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(学生被保存);}
} d. 用法 在set方法或者属性名前增加 resource(namestuDaoImpl)推荐增加名称默认是依照类型进行查找。比如 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)//声明资源
public class StudentService {
private StudentDao studentDao;
public StudentDao getStudentDao() {return studentDao;
}
Resource(namestuDaoImpl)//注入
public void setStudentDao(StudentDao studentDao) {this.studentDao studentDao;
}
public void add(Student s)
{this.studentDao.StudentSave(s);
}e.測试方式依旧和之前的注入方式一样注意这里的 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(程序执行之前....);ApplicationContext ctxnew ClassPathXmlApplicationContext(beans.xml);System.out.println(程序执行開始....);StudentService sService(StudentService)ctx.getBean(studentService);Student student(Student)ctx.getBean(student);Student student2(Student)ctx.getBean(student);System.out.println(student2student);sService.add(student);}
}f. 在jsp页面须要处理业务有java代码须要spring注入service。 须要如今想要获取的类前加标签 Component(docrelationService)首先jsp页面导入 %page importorg.springframework.context.ApplicationContext% %pageimportorg.springframework.web.context.support.WebApplicationContextUtils% 获取spring注入 % 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中用代码 ABeanFactory.getBean(a)得到A实例,就可以.不推荐会产生两个beanFactory由于在类B中须要用BeanFactory,所以我们必须在B中new一个 c)类B创建实例时候假设採用a方法决不能採用B bnew B();的方式否则会报nullpoint异常。 d)ClassPathXmlApplicationContext建立在beanFactory基础之上非常少有人直接使用。 測试代码 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(程序执行之前....);//BeanFactory ctxnew ClassPathXmlApplicationContext(beans.xml);//ClassPathXmlApplicationContext建立在beanFactory基础之上非常少有人直接使用。 ApplicationContext ctxnew ClassPathXmlApplicationContext(beans.xml);//直接获取spring容器 System.out.println(程序执行開始....); StudentService sService(StudentService)ctx.getBean(studentService);//StudentService相当于类B //StudentService sServicenew StudentService(); Student student(Student)ctx.getBean(student); Student student2(Student)ctx.getBean(student); System.out.println(student2student); sService.add(student); } } 版权声明本文博客原创文章博客未经同意不得转载。 转载于:https://www.cnblogs.com/zfyouxi/p/4640478.html