维护网站建设空间出租,哪些大型网站用mysql,丰台手机网站建设,德州网站建设推广Eclipse下Maven的集成 2.1指定本地maven环境 参考#xff1a;Eclipse的Maven创建_叶书文的博客-CSDN博客_eclipse创建maven项目 指定用本地maven指定maven仓库设置和地址2.2创建maven项目 1.新建 2.目录设置 3.坐标设置#xff08;随便写就行#xff09; 4.目录结构 2.3配置… Eclipse下Maven的集成 2.1指定本地maven环境 参考Eclipse的Maven创建_叶书文的博客-CSDN博客_eclipse创建maven项目 指定用本地maven 指定maven仓库设置和地址 2.2创建maven项目 1.新建 2.目录设置 3.坐标设置随便写就行 4.目录结构 2.3配置pom 导入spring的依赖 把代码直接复制进去 propertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/properties dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.1.16.RELEASE/version/dependency/dependencies2.4src/main/java中创建Java代码 1.创建的目录结构如下 1.entity代码 package test.entity;import java.util.Date;
import java.util.List;public class User {private int id;private String name;private String password;private int age;private Date birthday; private OtherBean otherBean; private ListString favorites; public int getId() {return id;} public void setId(int id) {this.id id;} public String getName() {return name;} public void setName(String name) {this.name name;} public String getPassword() {return password;} public void setPassword(String password) {this.password password;} public int getAge() {return age;} public void setAge(int age) {this.age age;} public Date getBirthday() {return birthday;} public void setBirthday(Date birthday) {this.birthday birthday;} public OtherBean getOtherBean() {return otherBean;} public void setOtherBean(OtherBean otherBean) {this.otherBean otherBean;} public ListString getFavorites() {return favorites;} public void setFavorites(ListString favorites) {this.favorites favorites;}
} package test.entity;public class OtherBean {private int id;public int getId() {return id;}public void setId(int id) {this.id id;}
} 2.dao代码 package test.dao;public class UserDao { public void login() {System.out.println(查询数据库成功);}} 3.service代码 package test.service;import test.dao.UserDao;public class UserService { private UserDao userDao;
// 内部方法public void login() {System.out.println(执行业务操作....);userDao.login();}// get和setpublic void setUserDao(UserDao userDao) {this.userDao userDao;} public UserDao getUserDao() {return userDao;}}2.5创建beans.xml beans.xml是Spring的配置文件这是采用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.xsddefault-autowireno !-- 设置autowireno不使用自动装配即使用手工装配 --!-- bean iduserService classservice.UserService autowireno--!-- property nameuserDao refuserDao/--!-- /bean--!-- bean iduserDao classdao.UserDao/-- !-- 设置autowirebyName使用Bean名字进行自动装配 --!-- bean iduserService classservice.UserService autowirebyName/--!-- bean iduserDao classdao.UserDao/-- !-- 设置autowirebyType使用Bean类型进行自动装配 --bean iduserService classtest.service.UserService autowirebyType/bean iduserDao classtest.dao.UserDao/
/beans 2.6运行启动类测试 1启动类代码 package test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import test.service.UserService;public class SpringTest {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);UserService userService (UserService) context.getBean(userService);System.out.println(userService.getUserDao());System.out.println(userServicenull);userService.login();}
} 运行成功说明bean注入成功