Spring之一 HelloWorld

1、导入jar包

c3p0-0.9.1.2.jar

mysql-connector-java-5.1.7-bin.jar

commons-logging-1.1.3.jar

spring-aop.4.0.0.REALESE.jar

spring-aspects-4.0.0.REALESE.jar

spring-beans-4.0.0.REALESE.jar

spring-context-4.0.0.REALESE.jar

spring-core-4.0.0.REALESE.jar

spring-expression-4.0.0.REALESE.jar

spring-jdbc-4.0.0.REALESE.jar

spring-tx-4.0.0.REALESE.jar

2、Bean

<span style="font-size:14px;">	public class Employees {
		private String uid;
		private String uname;

		public String getUid() {
			return uid;
		}

		public void setUid(String uid) {
			this.uid = uid;
		}

		public String getUname() {
			return uname;
		}

		public void setUname(String uname) {
			this.uname = uname;
		}

		public Employees() {
		}

		public Employees(String uid, String uname) {
			this.uid = uid;
			this.uname = uname;
		}
	}</span>

3、配置文件applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	">
		<bean id="employees" class="springstep2.ithings.entity.Employees">
			<property name="uid" value="10001"/>
			<property name="uname" value="hanmeimei"/>
		</bean>
	</beans>

4、测试方法

    public static void main(String[] args) {
        //创建Spring的IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //从容器中获取bean实例
        Employees employees = (Employees) applicationContext.getBean("employees");
        System.out.println(employees.getUid());
        System.out.println(employees.getUname());
    }

5、基于注解的形式配置bean

1)在spring配置文件中设置<context:component-scan>

    <context:component-scan base-package="com.ithings.wp">
        <context:include-filter type="regex" expression="com.ithings.wp.*.controller.*" />
    </context:component-scan>

--base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类. 

--当需要扫描多个包时, 可以使用逗号分隔.

--如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:

          <context:component-scan base-package="com.ithings.wp" resource-pattern="controller/*"/>

--<context:include-filter> 子节点表示要包含的目标类

--<context:exclude-filter> 子节点表示要排除在外的目标类

--<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点


2)特定组件包括:

@Component: 基本注解, 标识了一个受 Spring 管理的组件

@Respository: 标识持久层组件

@Service: 标识服务层(业务层)组件

@Controller: 标识表现层组件

@Autowired 和 @Resource 、@Inject


6.在web环境中使用spring

Spring之六 在Web环境中使用Spring


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值