长春电商网站建设多少钱,云虚拟主机安装wordpress,深圳营销型网站开发,餐饮连锁网站建设文章目录 Spring中基于注解的IOC配置项目举例详解1、创建如下结构的Spring项目pom.xmldao层service层application.xmllog4j.properties 2、用于创建对象的常用注解2.1、Controller或Controller(user)声明bean,且iduser2.2、Service或用Service(u… 文章目录 Spring中基于注解的IOC配置项目举例详解1、创建如下结构的Spring项目pom.xmldao层service层application.xmllog4j.properties 2、用于创建对象的常用注解2.1、Controller或Controller(user)声明bean,且iduser2.2、Service或用Service(user)声明bean且iduser2.3、Repository或同上面两个2.4、Component2.5、Scope以上几个注解功能相同但为了在项目实现过程中便于区分则一般按照下面分类使用 3、用于属性注入的注解3.1、Autowired3.2、Resource3.3、Value 测试类 Spring中基于注解的IOC配置项目举例详解
1、创建如下结构的Spring项目 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdSpringDemo/artifactIdgroupIdcn.fpl/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdSpring_IOC_Annotation/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.1.8.RELEASE/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.30/version/dependency/dependencies
/projectdao层
public class UserDaoImpl implements UserDao {Overridepublic void addUser(){System.out.println(insert into tb_user......);}
}service层
public class UserServiceImpl implements UserService {Overridepublic void addUser(){System.out.println(userDao name age);userDao.addUser();}
}application.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd !--告诉spring容器启动时要扫描的包且spring会依据注解创建对象并存到IOC容器中--context:component-scan base-packagecn.fpl/context:component-scan
/beanslog4j.properties
# Global logging configuration
log4j.rootLoggerDEBUG, stdout
# Console output...
log4j.appender.stdoutorg.apache.log4j.ConsoleAppender
log4j.appender.stdout.layoutorg.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern%5p [%t] - %m%n2、用于创建对象的常用注解
2.1、Controller或Controller(“user”)声明bean,且id“user” 作用 把资源交给spring来管理相当于bean id class一般用于表现层。 属性 value指定bean的id如果不指定value属性默认bean的id是当前类的类名首字母小写
2.2、Service或用Service(“user”)声明bean且id“user” 作用 把资源交给spring来管理相当于bean id class一般用于业务层。 属性 value指定bean的id如果不指定value属性默认bean的id是当前类的类名首字母小写
2.3、Repository或同上面两个 作用 把资源交给spring来管理相当于bean id class一般用于持久层。 属性 value指定bean的id如果不指定value属性默认bean的id是当前类的类名首字母小写 2.4、Component 作用 把资源交给spring来管理相当于bean id class通用。 属性 value指定bean的id如果不指定value属性默认bean的id是当前类的类名首字母小写 2.5、Scope 作用 指定bean的作用域范围。 属性 value指定范围的值singleton prototype request session。 以上几个注解功能相同但为了在项目实现过程中便于区分则一般按照下面分类使用 Controllerweb Serviceservice Repositorydao Component三层架构之外 3、用于属性注入的注解
以下四个注解的作用相当于property name ref。
3.1、Autowired
作用
自动按照类型注入。set方法可以省略。
案例
Service
public class UserServiceImpl implements UserService {Autowired //注入类型为UserDAO的beanprivate UserDao userDao;public void addUser(){userDao.addUser();}
}3.2、Resource 作用 自动按照名字注入。set方法可以省略。 属性
name指定bean的id。
案例
Service
public class UserServiceImpl implements UserService {Resource(nameuserDaoImpl)//注入id“userDaoImpl”的beanprivate UserDao userDao;public void addUser(){userDao.addUser();}
}3.3、Value 作用 注入基本数据类型和String类型数据的 属性
value用于指定值
案例一
Service
public class UserServiceImpl implements UserService {Resource(nameuserDaoImpl) //注入id“userDaoImpl”的beanprivate UserDao userDao;Value(张三)//注入Stringprivate String name;Value(18)//注入Integerprivate Integer age;public void addUser(){System.out.println(name,age);userDao.addUser();}
}案例二创建config.properties文件在resources文件夹下
name张三
age18在application.xml文件中加载配置文件
!--加载config.properties配置文件并把配置文件中的数据存到IOC容器中--context:property-placeholder locationconfig.properties/context:property-placeholder注入属性值
Service
public class UserServiceImpl implements UserService {Autowiredprivate UserDao userDao;Value(${name})//注入Stringprivate String name;Value(${age})//注入Integerprivate Integer age;public void addUser() {System.out.println(name,age);userDao.addUser();}
}测试类
public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);UserDao userDao ac.getBean(userDaoImpl, UserDao.class);UserService userService ac.getBean(user, UserService.class);userService.addUser();}