win 10 家庭版没有本地安全策略的添加方法及所在位置路径

 windows  win 10 家庭版没有本地安全策略的添加方法及所在位置路径已关闭评论
7月 162022
 

win 10 家庭版默认是没有本地安全策略的, 但可以通过下面方式添加。

  1. 新建一个bat文件,内容如下:

@echo off

pushd “%~dp0″

dir /b C:\Windows\servicing\Packages\Microsoft-Windows-GroupPolicy-ClientExtensions-Package~3*.mum >List.txt

dir /b C:\Windows\servicing\Packages\Microsoft-Windows-GroupPolicy-ClientTools-Package~3*.mum >>List.txt

for /f %%i in (‘findstr /i . List.txt 2^>nul’) do dism /online /norestart /add-package:”C:\Windows\servicing\Packages\%%i”

pause

2. 以管理员身份运行,运行过程中可能会看到它反复安装好几次,没关系,等提示“按任意键继续”后按键结束, 安装完毕。

 

“本地安全策略”位置在哪里?

打开“控制面板”, 将“查看方式”调整为“大图标”,找到“管理工具”并点击, 就能看到“”“本地安全策略”了

DONE!

 

ubuntu安装、查看已安装包的方法(转)

 ubuntu  ubuntu安装、查看已安装包的方法(转)已关闭评论
9月 162021
 

网上发现的一篇很好的关于ubuntu下查看或安装包的方法的文章,转载分享下,原文链接见文末。

 

说明:由于图形化界面方法(如Add/Remove… 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装、卸载和删除的方法。

一、Ubuntu中软件安装方法

1、APT方式

(1)普通安装:apt-get install softname1 softname2 …;

(2)修复安装:apt-get -f install softname1 softname2… ;(-f Atemp to correct broken dependencies)

(3)重新安装:apt-get –reinstall install softname1 softname2…;

2、Dpkg方式

(1)普通安装:dpkg -i package_name.deb

3、源码安装(.tar、tar.gz、tar.bz2、tar.Z)

首先解压缩源码压缩包然后通过tar命令来完成

a.解xx.tar.gz:tar zxf xx.tar.gz
b.解xx.tar.Z:tar zxf xx.tar.Z
c.解xx.tgz:tar zxf xx.tgz
d.解xx.bz2:bunzip2 xx.bz2
e.解xx.tar:tar xf xx.tar

然后进入到解压出的目录中,建议先读一下README之类的说明文件,因为此时不同源代码包或者预编译包可能存在差异,然后建议使用ls -F –color或者ls -F命令(实际上我的只需要 l 命令即可)查看一下可执行文件,可执行文件会以*号的尾部标志。

一般依次执行./configure

make

sudo make install

即可完成安装。

二、Ubuntu中软件包的卸载方法

1、APT方式

(1)移除式卸载:apt-get remove softname1 softname2 …;(移除软件包,当包尾部有+时,意为安装)

(2)清除式卸载 :apt-get –purge remove softname1 softname2…;(同时清除配置)

清除式卸载:apt-get purge sofname1 softname2…;(同上,也清除配置文件)

2、Dpkg方式

(1)移除式卸载:dpkg -r pkg1 pkg2 …;

(2)清除式卸载:dpkg -P pkg1 pkg2…;

 

三、Ubuntu中软件包的查询方法

Dpkg 使用文本文件来作为数据库.通称在 /var/lib/dpkg 目录下. 通称在 status 文件中存储软件状态,和控制信息. 在 info/ 目录下备份控制文件, 并在其下的 .list 文件中记录安装文件清单, 其下的 .mdasums 保存文件的 MD5 编码.

体验使用数据库的时刻到了:

$ dpkg -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad) ||/ Name Version Description +++-===========-================-======================================== ii aalib1 1.4p5-28 ascii art library - transitional package ii adduser 3.85 Add and remove users and groups ii alien .63 install non-native packages with dpkg ... ... 

每条记录对应一个软件包, 注意每条记录的第一, 二, 三个字符. 这就是软件包的状态标识, 后边依此是软件包名称, 版本号, 和简单描述.

  • 第一字符为期望值,它包括:
    • u 状态未知,这意味着软件包未安装,并且用户也未发出安装请求.
    • i 用户请求安装软件包.
    • r 用户请求卸载软件包.
    • p 用户请求清除软件包.
    • h 用户请求保持软件包版本锁定.
  • 第二列,是软件包的当前状态.此列包括软件包的六种状态.
    • n 软件包未安装.
    • i 软件包安装并完成配置.
    • c 软件包以前安装过,现在删除了,但是它的配置文件还留在系统中.
    • u 软件包被解包,但还未配置.
    • f 试图配置软件包,但是失败了.
    • h 软件包安装,但是但是没有成功.
  • 第三列标识错误状态,可以总结为四种状态. 第一种状态标识没有问题,为空. 其它三种符号则标识相应问题.
    • h 软件包被强制保持,因为有其它软件包依赖需求,无法升级.
    • r 软件包被破坏,可能需要重新安装才能正常使用(包括删除).
    • x 软包件被破坏,并且被强制保持.

也可以以统配符模式进行模糊查询, 比如我要查找以nano字符开始的所有软件包:

$ dpkg -l nano* Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed |/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad) ||/ Name Version Description +++-==============-==============-============================================ ii nano 1.3.10-2 free Pico clone with some new features pn nano-tiny <none> (no description available) un nanoblogger <none> (no description available) 

以上状态说明: 系统中安装了 nano 版本为 1.3.10-2 ;安装过 nano-tiny , 后来又清除了; 从未安装过nanoblogger .

如果觉得 dpkg 的参数过多, 不利于记忆的话, 完全可以使用 dpkg-query 进行 dpkg 数据库查询.

应用范例:

  • 查询系统中属于nano的文件:

    $ dpkg –listfiles nano

    or

    $ dpkg-query -L nano

  • 查看软件nano的详细信息:

    $ dpkg -s nano

    or

    $ dpkg-query -s nano

  • 查看系统中软件包状态, 支持模糊查询:

    $ dpkg -l

    or

    $dpkg-query -l

  • 查看某个文件的归属包:

    $ dpkg-query -S nano

    or

    $ dpkg -S nano

三、其他应用总结

apt-cache search # ——(package 搜索包)
apt-cache show #——(package 获取包的相关信息,如说明、大小、版本等)
apt-get install # ——(package 安装包)
apt-get install # —–(package –reinstall 重新安装包)
apt-get -f install # —–(强制安装, “-f = –fix-missing”当是修复安装吧…)
apt-get remove #—–(package 删除包)
apt-get remove –purge # ——(package 删除包,包括删除配置文件等)
apt-get autoremove –purge # —-(package 删除包及其依赖的软件包+配置文件等(只对6.10有效,强烈推荐))
apt-get update #——更新源
apt-get upgrade #——更新已安装的包
apt-get dist-upgrade # ———升级系统
apt-get dselect-upgrade #——使用 dselect 升级
apt-cache depends #——-(package 了解使用依赖)
apt-cache rdepends # ——(package 了解某个具体的依赖,当是查看该包被哪些包依赖吧…)
apt-get build-dep # ——(package 安装相关的编译环境)
apt-get source #——(package 下载该包的源代码)
apt-get clean && apt-get autoclean # ——–清理下载文件的存档 && 只清理过时的包
apt-get check #——-检查是否有损坏的依赖
dpkg -S filename —–查找filename属于哪个软件包
apt-file search filename —–查找filename属于哪个软件包
apt-file list packagename —–列出软件包的内容
apt-file update –更新apt-file的数据库

dpkg –info “软件包名” –列出软件包解包后的包名称.
dpkg -l –列出当前系统中所有的包.可以和参数less一起使用在分屏查看. (类似于rpm -qa)
dpkg -l |grep -i “软件包名” –查看系统中与”软件包名”相关联的包.
dpkg -s 查询已安装的包的详细信息.
dpkg -L 查询系统中已安装的软件包所安装的位置. (类似于rpm -ql)
dpkg -S 查询系统中某个文件属于哪个软件包. (类似于rpm -qf)
dpkg -I 查询deb包的详细信息,在一个软件包下载到本地之后看看用不用安装(看一下呗).
dpkg -i 手动安装软件包(这个命令并不能解决软件包之前的依赖性问题),如果在安装某一个软件包的时候遇到了软件依赖的问题,可以用apt-get -f install在解决信赖性这个问题.
dpkg -r 卸载软件包.不是完全的卸载,它的配置文件还存在.
dpkg -P 全部卸载(但是还是不能解决软件包的依赖性的问题)
dpkg -reconfigure 重新配置

apt-get install
下载软件包,以及所有依赖的包,同时进行包的安装或升级。如果某个包被设置了 hold (停止标志,就会被搁在一边(即不会被升级)。更多 hold 细节请看下面。
apt-get remove [–purge]
移除 以及任何依赖这个包的其它包。
–purge 指明这个包应该被完全清除 (purged) ,更多信息请看 dpkg -P。

apt-get update
升级来自 Debian 镜像的包列表,如果你想安装当天的任何软件,至少每天运行一次,而且每次修改了
/etc/apt/sources.list 後,必须执行。

apt-get upgrade [-u]
升 级所有已经安装的包为最新可用版本。不会安装新的或移除老的包。如果一个包改变了依赖关系而需要安装一个新的包,那么它将不会被升级,而是标志为 hold。apt-get update 不会升级被标志为 hold 的包 (这个也就是 hold 的意思)。请看下文如何手动设置包为 hold。我建议同时使用 ‘-u’ 选项,因为这样你就能看到哪些包将会被升级。

apt-get dist-upgrade [-u]
和 apt-get upgrade 类似,除了 dist-upgrade 会安装和移除包来满足依赖关系。因此具有一定的危险性。

apt-cache search
在软件包名称和描述中,搜索包含xxx的软件包。

apt-cache show
显示某个软件包的完整的描述。

apt-cache showpkg
显示软件包更多细节,以及和其它包的关系。

dselect
console-apt
aptitude
gnome-apt
APT 的几个图形前端(其中一些在使用前得先安装)。这里 dselect 无疑是最强大的,也是最古老,最难驾驭。

普通 Dpkg 用法
dpkg -i
安装一个 Debian 包文件,如你手动下载的文件。

dpkg -c
列出 的内容。

dpkg -I
从 中提取包信息。

dpkg -r
移除一个已安装的包。

dpkg -P
完全清除一个已安装的包。和 remove 不同的是,remove 只是删掉数据和可执行文件,purge 另外还删除所有的配制文件。

dpkg -L
列出 安装的所有文件清单。同时请看 dpkg -c 来检查一个 .deb 文件的内容。

dpkg -s
显示已安装包的信息。同时请看 apt-cache 显示 Debian 存档中的包信息,以及 dpkg -I 来显示从一个 .deb 文件中提取的包信息。

dpkg-reconfigure
重 新配制一个已经安装的包,如果它使用的是 debconf (debconf 为包安装提供了一个统一的配制界面)。你能够重新配制 debconf 它本身,如你想改变它的前端或提问的优先权。例如,重新配制 debconf,使用一个 dialog 前端,简单运行:

dpkg-reconfigure –frontend=dialog debconf (如果你安装时选错了,这里可以改回来哟:)

echo ” hold” | dpkg –set-selections
设置 的状态为 hlod (命令行方式)

dpkg –get-selections “”
取的 的当前状态 (命令行方式)

支持通配符,如:
Debian:~# dpkg –get-selections *wine*
libwine hold
libwine-alsa hold
libwine-arts hold
libwine-dev hold
libwine-nas hold
libwine-print hold
libwine-twain hold
wine hold
wine+ hold
wine-doc hold
wine-utils hold

例如:
大家现在用的都是 gaim-0.58 + QQ-plugin,为了防止 gaim 被升级,我们可以采用如下方法:

方法一:
Debian:~# echo “gaim hold” | dpkg –set-selections
然後用下面命令检查一下:
Debian:~# dpkg –get-selections “gaim”
gaim hold
现在的状态标志是 hold,就不能被升级了。

如果想恢复怎么办呢?
Debian:~# echo “gaim install” | dpkg –set-selections
Debian:~# dpkg –get-selections “gaim”
gaim install
这时状态标志又被重置为 install,可以继续升级了。

同志们会问,哪个这些状态标志都写在哪个文件中呢?
在 /var/lib/dpkg/status 里,你也可以通过修改这个文件实现 hold。

有时你会发现有的软件状态标志是 purge,不要奇怪。
如:事先已经安装了 amsn,然後把它卸了。
apt-get remove –purge amsn
那么状态标志就从 install 变成 purge。

方法二:
在/etc/apt 下手动建一个 preferences 文件
内容:
Package: gaim
Pin: version 0.58*
保存

dpkg -S
在包数据库中查找 ,并告诉你哪个包包含了这个文件。(注:查找的是事先已经安装的包)

——————————————–
Debian的软件包管理工具命令不完全列表
——————————————–
Debian系统中所有的包信息都在/var/lib/dpkg下.其中/var/lib/dpkg/info目录中保存了各个软件包的信息及管理文件.每个文件的作用如下:
以 “.conffiles”     结尾的文件记录软件包的配置列表.
以 “.list”          结尾的文件记录了软件包的文件列表,用户可在文件当中找到软件包文件的具体安装位置.
以 “.md5sums”       结尾的文件记录了md5信息,用来进行包的验证的.
以 “.config”        结尾的文件是软件包的安装配置角本.
以 “.postinst”      角本是完成Debian包解开之后的配置工作,通常用来执行所安装软件包相关的命令和服务的重新启动.
以 “.preinst”       角本在Debain解包之前运行,主要作用是是停止作用于即将升级的软件包服务直到软件包安装或和升级完成.
以 “.prerm”         脚本负责停止与软件包关联的daemon服务,在删除软件包关联文件之前执行.
以 “.postrm”        脚本负责修改软件包链接或文件关联,或删除由它创建的文件.

/var/lib/dpkg/available是软件包的描述信息.
包括当前系统中所有使用的Debian安装源中所有的软件包,还包括当前系统中已经安装和未安装的软件包.

1.dpkg包管理工具

dpkg -r 卸载软件包.不是完全的卸载,它的配置文件还存在.
dpkg –info “软件包名” –列出软件包解包后的包名称.
dpkg -l     –列出当前系统中所有的包.可以和参数less一起使用在分屏查看.
dpkg -l |grep -i “软件包名” –查看系统中与”软件包名”相关联的包.
dpkg -s   查询已安装的包的详细信息. dpkg -L   查询系统中已安装的软件包所安装的位置.
dpkg -S   查询系统中某个文件属于哪个软件包.
dpkg -I   查询deb包的详细信息,在一个软件包下载到本地之后看看用不用安装(看一下呗).
dpkg -i 手动安装软件包(这个命令并不能解决软件包之前的依赖性问题),如果在安装某一个软件包的时候遇到了软件依赖的问题,可以用apt-get -f install在解决信赖性这个问题.
dpkg -reconfigure 重新配置
dpkg -P 全部卸载(但是还是不能解决软件包的依赖性的问题)

 

2. apt高级包管理工具
(1)GTK图形的”synaptic”,这是APT的前端工具.
(2)”aptitude”,这也是APT的前端工具.
用APT管理工具进行包的管理,可以有以下几种方法做源:
(1)拿安装盘做源,方法如下:
apt-cdrom ident        扫描光盘的信息
apt-cdrom add          添加光盘源
(2)这也是最常用的方法就是把源添加到/etc/apt/source.list中,之后更新列apt-get update

APT管理工具常用命令
apt-cache 加上不同的子命令和参数的使用可以实现查找,显示软件,包信息及包信赖关系等功能.
apt-cache stats 显示当前系统所有使用的Debain数据源的统计信息.
apt-cache search +”包名”,可以查找相关的软件包.
apt-cache show   +”包名”,可以显示指定软件包的详细信息.
apt-cache depends +”包名”,可以查找软件包的依赖关系.
apt-get upgrade   更新系统中所有的包到最新版
apt-get install   安装软件包
apt-get –reindtall install 重新安装软件包
apt-get remove 卸载软件包
apt-get –purge remove 完全卸载软件包
apt-get clean 清除无用的软件包
在用命令apt-get install之前,是先将软件包下载到/var/cache/apt/archives中,之后再进行安装的.所以我们可以用apt-get clean清除/var/cache/apt/archives目录中的软件包.

源码包安装
apt-cache showsrc 查找看源码包的文件信息(在下载之前)
apt-get source 下载源码包.
apt-get build-dep +”包名” 构建源码包的编译环境.

 

清除处于rc状态的软件包

dpkg -l |grep ^rc|awk ‘{print $2}’ |tr [“\n”] [” “] | sudo xargs dpkg -P –

 

转自: http://www.cnblogs.com/forward/archive/2012/01/10/2318483.html

 

Mac无法启动过程中出现的各类符号及处理方法(禁止,圆圈,问号,齿轮,空白,警告,地球等)

 mac, macpro  Mac无法启动过程中出现的各类符号及处理方法(禁止,圆圈,问号,齿轮,空白,警告,地球等)已关闭评论
2月 252021
 

Mac无法启动过程中出现的各类符号及处理方法,此文摘录自apple官网。

 

如果您的 Mac 无法顺利完成启动,或者在启动过程中需要您完成某项操作,可能会卡在以下某个屏幕上。

由直线穿过的圆圈

macOS 启动屏幕上显示了禁止符号和 URL:support.apple.com/mac/startup

了解如果您的 Mac 在启动时显示一个由直线穿过的圆圈(禁止符号),该怎么做。


问号


“选项”齿轮图标


空白屏幕

Mac 笔记本电脑空白灰屏

了解如果您的 Mac 在启动时出现空白屏幕(例如空白灰色、黑色或蓝色屏幕),该怎么做。



带有警告符号的地球

macOS 启动屏幕上显示了带有警告符号的地球和 URL:support.apple.com/mac/startup

如果显示带有感叹号的地球,则说明您的 Mac 曾尝试通过互联网从 macOS 恢复功能启动,但失败了。了解如果您的 Mac 无法从 macOS 恢复功能启动,该怎么做。


锁形图标

macOS 启动屏幕上显示了固件锁形图标和密码输入栏

如果您的 Mac 使用了固件密码,则当您尝试从另一个磁盘或宗卷(例如,外置驱动器或 macOS 恢复功能)启动时会出现锁形图标。请输入固件密码以继续。


锁定系统的 PIN 码

macOS 锁定系统的 PIN 码启动屏幕

如果您的 Mac 已通过“查找”功能进行了远程锁定,则系统将要求输入 PIN 码。请输入四位数或六位数密码以继续。


登录窗口

macOS Big Sur 用户登录屏幕

在登录窗口中,输入您的用户帐户密码,以登录您的 Mac。如果“文件保险箱”已打开,则这一操作还会解锁您的磁盘。您可能会在背景中看到默认的桌面图片,在您选择自己的帐户后,这个图片可能会更改为您选择的桌面图片。


雷雳、USB 或 FireWire 符号

如果屏幕上显示黑色背景和大的雷雳 、USB  或 FireWire  符号,则表示您的 Mac 处于目标磁盘模式

springboot jar包解压、重新压缩打包方法

 jar, spring-boot  springboot jar包解压、重新压缩打包方法已关闭评论
5月 112020
 

springboot在产生jar包后,有时需要修改其中的文件并重新打包,比如上传到服务器后,发现配置文件里某个配置没改,可以使用下面的方法:

以一个测试springboot 包  spingboot-ouu-test.jar 为例

因为有重新打包的需求,建议jar包放在一个空的文件夹下执行下面的命令:

  • 解压spingboot-ouu-test.jar

jar -xvf spingboot-ouu-test.jar

常见的配置文件如:application.properties在BOOT-INF/classes/目录下.

  • 修改文件后, 重新打包spingboot-ouu-test.jar,

jar  -cvfM0 spingboot-ouu-test.jar .           //(M后面是数字0,如果只使用参数cf,java -jar spingboot-ouu-test.jar启动springboot jar项目时将提示“没有主清单属性”)

 

附:

jar -tvf spingboot-ouu-test.jar   //查看jar包文件结构

jar命令常用参数:

c :创建一个 jar 包
t :显示 jar 包里面的内容
x :解压 jar 包
u :添加文件到 jar包
f :指定 jar 包的文件名
v :在 CMD 显示详细执行过程(报告)
m :指定 manufest.mf 文件(该文件可以对jar包及其内容做设置)
0 :打包 jar包 时不压缩
M :不产生 manufest.mf 文件,覆盖 m 参数的设置
i :为打包的 jar包 创建索引文件
c :进入某目录后再执行 jar 命令,相当于 cd 进入目录然后不带 c 参数执行 jar命令

 

mvn打包时出现“Could not transfer artifact xxx from/to central (http://repo1.maven.org/maven2/): Failed to transfer file … HTTPS Required. Return code is: 501 ” 解决办法

 MAVEN  mvn打包时出现“Could not transfer artifact xxx from/to central (http://repo1.maven.org/maven2/): Failed to transfer file … HTTPS Required. Return code is: 501 ” 解决办法已关闭评论
3月 092020
 

命令行mvn打包dubbo-admin时出现下面提示:

[INFO] Scanning for projects…

Downloading: http://repo1.maven.org/mavenDownloading: http://repo1.maven.org/mavenDownloading: http://repo1.maven.org/mavenDownloading: http://repo1.maven.org/mavenDownloading: http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.1.4.RELEASE/spring-boot-starter-parent-2.1.4.RELEASE.pom

[ERROR] The build could not read 1 project -> [Help 1]

[ERROR]   

[ERROR]   The project org.apache:dubbo-admin:0.1 (/Users/jqzhu/Documents/codes/dubbo-admin/pom.xml) has 1 error

[ERROR]     Non-resolvable import POM: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.1.4.RELEASE from/to central (http://repo1.maven.org/maven2/): Failed to transfer file: http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.1.4.RELEASE/spring-boot-starter-parent-2.1.4.RELEASE.pom. Return code is: 501 , ReasonPhrase:HTTPS Required. @ line 90, column 16 -> [Help 2]

[ERROR]

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR] Re-run Maven using the -X switch to enable full debug logging.

[ERROR]

[ERROR] For more information about the errors and possible solutions, please read the following articles:

[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException

[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

看来需要使用https代替http。

 

原因:

Effective January 15, 2020, The Central Repository no longer supports insecure communication over plain HTTP and requires that all requests to the repository are encrypted over HTTPS.

解决办法:

Replace http://repo1.maven.org/maven2/ with https://repo1.maven.org/maven2/

Replace http://repo.maven.apache.org/maven2/ with https://repo.maven.apache.org/maven2/

If for any reason your environment cannot support HTTPS, you have the option of using :  http://insecure.repo1.maven.org/maven2/

redis数据迁移的3个方法

 redis  redis数据迁移的3个方法已关闭评论
1月 172020
 

1. rdb数据备份恢复方法
redis 127.0.0.1:6379> SAVE
OK
或者
redis-cli -h 127.0.0.1 -p 6379 -a pwd bgsave
该命令将在 redis 安装目录中创建dump.rdb文件。

查找dump.rdb文件位置
redis 127.0.0.1:6379> CONFIG GET dir
1) dir
2) /usr/local/redis/bin
以上命令 CONFIG GET dir 输出的 redis 安装目录为 /usr/local/redis/bin
bgsave
创建 redis 备份文件也可以使用命令 BGSAVE,该命令在后台执行。
127.0.0.1:6379> BGSAVE
Background saving started

2. AOF数据备份恢复方法
另一种持久化方式AOF,在配置文件中打开[appendonly yes]。
AOF刷新日志到disk的规则:
appendfsync always #always 表示每次有写操作都进行同步,非常慢,非常安全。
appendfsync everysec #everysec表示对写操作进行累积,每秒同步一次
官方的建议的everysec,安全,就是速度不够快,如果是机器出现问题可能会丢失1秒的数据。

也可以手动执行bgrewriteaof进行AOF备份:
redis-cli -h 127.0.0.1 -p 6379 -a pwd bgrewriteaof

迁移数据恢复
迁移到另外一台恢复数据,需先检查配置文件,将按照以下优先级恢复数据到内存:
如果只配置AOF,重启时加载AOF文件恢复数据;
如果同时 配置了RBDAOF,启动是只加载AOF文件恢复数据;
如果只配置RBD,启动是讲加载dump文件恢复数据;

dump.rdb或者AOF文件迁移到另外一台恢复数据
恢复数据,只需将备份文件 (dump.rdb或者AOF文件) 移动到 redis 安装目录并启动服务即可。

3. redis从库复制数据方法
Redis提供了复制(replication)功能可以自动实现同步的过程。
配置方法
通过配置文件 从数据库的配置文件中加入slaveof master-ip master-port,主数据库无需配置
通过命令行参数 启动redis-server的时候,使用命令行参数–slaveof master-ip master port
redis-server –port 6380 –slaveof 127.0.0.1 6379
通过命令SLAVEOF master-ip master-port
redis>SLAVEOF 127.0.0.1 6379
SLAVEOF NO ONE可以是当前数据库停止接收其他数据库的同步,转成主Redis数据库,程序连接地址都改为新的redisIP地址和端口。

转自:https://my.oschina.net/ppabvc/blog/819448

XCODE中不同添加方式下获取文件/图片的路径的方法(Bundle.main.path,Bundle)

 swift, xcode  XCODE中不同添加方式下获取文件/图片的路径的方法(Bundle.main.path,Bundle)已关闭评论
12月 202019
 

XCODE中获取添加的文件的路径的3个方法,以下以显示图片测试获取图片路径的方法:

 

测试的UIViewController的demo源码(TestImageViewController.swift),注释中详细描述3个不同添加文件方式下获取路径的方法,GOOD LUCK!

 

import AVFoundation

 

class TestImageViewController: UIViewController {

    

    lazy var imageView: UIImageView = {

       

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))

        //方法1:  选择Targets中项目 –> build Phases中 –> copy Bundle Resources点击下面的+ 直接添加的alalay.jpg

        //let testImage = UIImage(contentsOfFile: Bundle.main.path(forResource: “alalay”, ofType: “jpg”)!)

        

        //方法2:  新建目录任意命名如imgs,将测试图片alalay.jpg文件拖入imgs目录后,将imgs目录名称修改为imgs.bundle,将这个imgs.bundle拖入xcode项目

        //let testImage = UIImage(contentsOfFile: Bundle.main.path(forResource: “imgs”, ofType: “bundle”)! + “/” + “alalay.jpg”)

        

        //方法3:   将alalay.jpg图片放在Assets.xcassets中

        let testImage = UIImage(named:“alalay.jpg”)!

        

        imageView.image = testImage

        imageView.contentMode = .scaleAspectFit

        return imageView

    }()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        view.addSubview(imageView)

    }

}

pip安装包到指定目录方法(指定路径)

 mac, pip, python  pip安装包到指定目录方法(指定路径)已关闭评论
10月 242019
 

mac中使用pip安装包后,可能会发现明明提示“Successfully installed xxx“,但就是找不到这个包“ImportError: No module named  xxx”?

其实是这样: 默认pip安装在/usr/local/lib/python2.7/site-packages, 但如果sudo pip安装则会安装在/lib/python2.7/site-packages,那如果我要指定安装包的路径需要怎么做呢?使用下面的黑体字命令即可,在-install-option里添加–install-purelib指定目录即可,以下命令安装google-api-python-client包到/usr/local/lib/python2.7/site-packages

pip install --install-option="--install-purelib=/usr/local/lib/python2.7/site-packages" google-api-python-client

 

DONE!!!

Mybatis中实体类属性和数据列之间映射的四种办法

 开发  Mybatis中实体类属性和数据列之间映射的四种办法已关闭评论
12月 292018
 

网上总结的4个方法,挺好的,分享下: https://blog.csdn.net/lmy86263/article/details/53150091

首先先定义一个测试实体类,如下:

public class User implements Serializable {
    private Integer userId;
    private String userName;
    ……
}

1. 通过XML映射文件中的resultMap

这种方式是最常见的,类似如下:

<mapper namespace=”data.UserMapper”>
    <resultMap type=”data.User” id=”userResultMap”>
        <!– 用id属性来映射主键字段 –>
        <id property=”id” column=”user_id”/>
        <!– 用result属性来映射非主键字段 –>
        <result property=”userName” column=”user_name”/>
    </resultMap>
</mapper>
通过里面的id标签和result标签来建立映射关系,由property和column分别指定实体类属性和数据表的列名。

2. 通过注解@Results和@Result

这两个注解是与XML文件中的标签相对应的:

@Results对应resultMap
@Result对应result
这两个注解是应用在方法的级别上的,也就是在mapper方法上,如下:

@Select(“select * from t_user where user_name = #{userName}”)
@Results(
        @Result(property = “userId”, column = “user_id”),
        @Result(property = “userName”, column = “user_name”)
)
User getUserByName(@Param(“userName”) String userName);
缺点:

由于注解是针对方法的,对于Mapper中的每个操作数据库的方法都必须有相同的注解完成映射关系的建立,导致很多的配置是重复的;
如果要避免配置重复的问题,可以采用在XML配置文件中配置这个resultMap,然后再@Result中通过id属性引用这个resultMap, 
但是这样感觉很麻烦(由于使用了两种配置方式),不如直接使用基于XML的resultMap配置方式;
3. 通过属性配置完成映射

使用者最陌生的是通过配置属性来完成映射,Mybatis给我们提供了一种映射方式,如果属性的命名是遵从驼峰命名法的,数据列名遵从下划线命名, 
那么可以使用这种方式,类似如下:

userName对应user_name;
userId对应user_id;
配置代码如下:

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
Configuration configuration = new Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);

4. 通过使用在SQL语句中定义别名完成映射

这种方式最直接,直接在SQL语句中建立别名来完成映射,如下:

@Select(“select user_name as userName, user_id as userId from t_user where user_name = #{userName}”)
User getUserByName(@Param(“userName”) String userName);

Javascript 创建对象方法的总结

 javascript  Javascript 创建对象方法的总结已关闭评论
7月 142017
 

网上找到的资料,记录下,免得下次再找了,http://www.cnblogs.com/wangjq/p/3755691.html

使用Javascript创建对象的方法有很多,现在就来列举一下:

1. 使用Object构造函数来创建一个对象,下面代码创建了一个person对象,并用两种方式打印出了Name的属性值。

    var person = new Object();
    person.name="kevin";
    person.age=31;
    alert(person.name);
    alert(person["name"])

2. 使用对象字面量创建一个对象;不要奇怪person[“5”],这里是合法的;另外使用这种加括号的方式字段之间是可以有空格的如person[“my age”].

复制代码
    var person = {
        name:"Kevin",
        age:31, 5:"Test" };
    alert(person.name);  alert(person["5"]);
复制代码

3. 使用工厂模式创建对象,返回带有属性和方法的person对象。

复制代码
function createPerson(name, age,job)
{ var o = new Object();
    o.name=name;
    o.age=31;
    o.sayName=function()
    {
        alert(this.name);
    }; return o;
}
createPerson("kevin",31,"se").sayName();
复制代码

4. 使用自定义构造函数模式创建对象;这里注意命名规范,作为构造函数的函数首字母要大写,以区别其它函数。这种方式有个缺陷是sayName这个方法,它的每个实例都是指向不同的函数实例,而不是同一个。

复制代码
function Person(name,age,job)
{ this.name=name; this.age=age; this.job=job; this.sayName=function()
    {
        alert(this.name);
    };
} var person = new Person("kevin",31,"SE");
person.sayName();
复制代码

5. 使用原型模式创建对象;解决了方法4中提到的缺陷,使不同的对象的函数(如sayFriends)指向了同一个函数。但它本身也有缺陷,就是实例共享了引用类型friends,从下面的代码执行结果可以看到,两个实例的friends的值是一样的,这可能不是我们所期望的。

复制代码
function Person()
{

}

Person.prototype = {
    constructor : Person,
    name:"kevin",
    age:31,
    job:"SE",
    friends:["Jams","Martin"],
    sayFriends:function()
    {
        alert(this.friends);
    }
}; var person1 = new Person();
person1.friends.push("Joe");
person1.sayFriends();//Jams,Martin,Joe
var person2 = new Person(); 
person2.sayFriends();//James,Martin,Joe
复制代码

6. 组合使用原型模式和构造函数创建对象,解决了方法5中提到的缺陷,而且这也是使用最广泛、认同度最高的创建对象的方法。

复制代码
function Person(name,age,job)
{ this.name=name; this.age=age; this.job=job;
   this.friends=["Jams","Martin"];
}
Person.prototype.sayFriends=function()
{
    alert(this.friends);
}; var person1 = new Person("kevin",31,"SE");
var person2 = new Person("Tom",30,"SE");
person1.friends.push("Joe");
person1.sayFriends();//Jams,Martin,Joe
person2.sayFriends();//Jams,Martin
复制代码

 7. 动态原型模式;这个模式的好处在于看起来更像传统的面向对象编程,具有更好的封装性,因为在构造函数里完成了对原型创建。这也是一个推荐的创建对象的方法。

复制代码
function Person(name,age,job)
{ //属性 this.name=name; this.age=age; this.job=job; this.friends=["Jams","Martin"]; //方法 if(typeof this.sayName !="function")
    {
        Person.prototype.sayName=function()
        {
            alert(this.name);
        };
        
        Person.prototype.sayFriends=function()
        {
            alert(this.friends);
        };
    }
} var person = new Person("kevin",31,"SE");
person.sayName();
person.sayFriends();
复制代码

另外还有两个创建对象的方法,寄生构造函数模式和稳妥构造函数模式。由于这两个函数不是特别常用,这里就不给出具体代码了。

写了这么多创建对象的方法,其实真正推荐用的也就是方法6和方法7。当然在真正开发中要根据实际需要进行选择,也许创建的对象根本不需要方法,也就没必要一定要选择它们了。

Maven打包可执行Jar的几种方法

 MAVEN  Maven打包可执行Jar的几种方法已关闭评论
11月 292016
 

一、无依赖其他任何jar

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.think.TestMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

运行:mvn clean package,在target中找到打包出来的,命令后运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去

二、解决依赖其他包时,可执行jar的打包

1、

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.think.TestMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

但以上方式用的比较少,因为我们依赖的jar,也会打进到我们最终生成的jar,这样不太好,假如你生成的jar要给别人使用,最好给一个纯净的。

一般用assembly会再用他另外一个功能,将我们的jar归档,打包成一个zip

2、打成一个zip包,发布项目的时候,将zip包copy到服务器上,直接unzip xxx.zip,里面包含要运行到jar以及依赖的lib,还有配置的config文件,即可直接启动服务

<build>
        <resources>
            <!-- 控制资源文件的拷贝 -->
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}/classes</targetPath>
            </resource>
        </resources>
        <plugins>
            <!-- 设置源文件编码方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- The configuration of maven-jar-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Configuration of the archiver -->
                    <archive>

                        <!--
                            生成的jar中,不要包含pom.xml和pom.properties这两个文件
                        -->
                        <addMavenDescriptor>false</addMavenDescriptor>

                        <!-- Manifest specific configuration -->
                        <manifest>
                            <!--
                                是否要把第三方jar放到manifest的classpath中
                            -->
                            <addClasspath>true</addClasspath>
                            <!--
                               生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/
                           -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--
                                应用的main class
                            -->
                            <mainClass>com.think.TestMain</mainClass>
                        </manifest>
                    </archive>
                    <!--
                        过滤掉不希望包含在jar中的文件
                    -->
                    <!--<excludes>-->
                        <!--<exclude>${project.basedir}/xml/*</exclude>-->
                    <!--</excludes>-->
                </configuration>
            </plugin>

            <!-- The configuration of maven-assembly-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Specifies the configuration file of the assembly plugin -->
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

还有一个重要的文件,名字可以随便起,我上面用的是 src/main/assembly/assembly.xml

<assembly>
    <id>bin</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- 最终打包成一个用于发布的zip文件 -->
    <formats>
        <format>zip</format>
    </formats>

    <!-- Adds dependencies to zip package under lib directory -->
    <dependencySets>
        <dependencySet>
            <!--
               不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录
           -->
            <useProjectArtifact>false</useProjectArtifact>
            <!--<outputDirectory>lib</outputDirectory>-->
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <!--<fileSet>-->
            <!--<directory>${project.basedir}</directory>-->
            <!--<outputDirectory>/</outputDirectory>-->
        <!--</fileSet>-->

        <!-- 把项目的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${deploy.dir}/classes/</directory>
            <outputDirectory>/conf</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

最终执行命令:mvn clean package,出来的是这样的

解压zip包,我们看到我们想要的,good

3、还有一种打包方式,上面相当于把我们想要的东西打成一个zip包,全部放到一起,看着整洁好看,但有点繁琐,我们其实可以用另外一个插件来完成,不打包,即看到上图解压后的文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>myTestJar</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>myTestJar</name>
    <url>http://maven.apache.org</url>

    <properties>
        <deploy.dir>./target/</deploy.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>myTest</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <!-- 控制资源文件的拷贝 -->
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}</targetPath>
            </resource>
        </resources>
        <plugins>
            <!-- 设置源文件编码方式 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.think.TestMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 拷贝依赖的jar包到lib目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->
                            <excludeTransitive>true</excludeTransitive>
                            <!-- 复制的jar文件去掉版本信息 -->
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 解决资源文件的编码问题 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 打包source文件为jar文件 -->
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <attach>true</attach>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这里采用的是maven-dependency-plugin插件,进行资源的copy。

4、

<build>
        <resources>
            <resource>
                <targetPath>${project.build.directory}/classes</targetPath>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.think.TestMain</mainClass>
                                </transformer>
                                <!--<transformer-->
                                        <!--implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">-->
                                    <!--<resource>applicationContext.xml</resource>-->
                                <!--</transformer>-->
                            </transformers>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>executable</shadedClassifierName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这种方式打出来是柔和到一起,成为一个jar,

可以直接java -jar xxx.jar运行。

我们可以根据不同需要来打包,如果暴露给外面,可以采用第4种,如果是自己公司项目打包,建议2,3种,因为有时候只是改了个配置文件,不需要打包,直接把配置文件复制进去即可

转自:http://m.blog.csdn.net/article/details?id=51871705

python操作日期和时间的方法

 python  python操作日期和时间的方法已关闭评论
8月 162016
 

python操作日期和时间的方法

不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法。
1.将字符串的时间转换为时间戳

复制代码代码如下:
方法:
a = “2013-10-10 23:40:00”
#将其转换为时间数组
import time
timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
#转换为时间戳:
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600

2.格式更改
如a = “2013-10-10 23:40:00”,想改为 a = “2013/10/10 23:40:00”
方法:先转换为时间数组,然后转换为其他格式

复制代码代码如下:
timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
otherStyleTime = time.strftime(“%Y/%m/%d %H:%M:%S”, timeArray)

3.时间戳转换为指定格式日期
方法一:利用localtime()转换为时间数组,然后格式化为需要的格式,如:

复制代码代码如下:
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)
otherStyletime == “2013-10-10 23:40:00”

方法二:

复制代码代码如下:
import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime(“%Y-%m-%d %H:%M:%S”)
otherStyletime == “2013-10-10 23:40:00”

4.获取当前时间并转换为指定日期格式
方法一:

复制代码代码如下:
import time
#获得当前时间时间戳
now = int(time.time())  ->这是时间戳
#转换为其他日期格式,如:”%Y-%m-%d %H:%M:%S”
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)

方法二:

复制代码代码如下:
import datetime
#获得当前时间
now = datetime.datetime.now()  ->这是时间数组格式
#转换为指定的格式:
otherStyleTime = now.strftime(“%Y-%m-%d %H:%M:%S”)

5.获得三天前的时间的方法

复制代码代码如下:
import time
import datetime
#先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() – datetime.timedelta(days = 3))
#转换为时间戳:
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
#转换为其他字符串格式:
otherStyleTime = threeDayAgo.strftime(“%Y-%m-%d %H:%M:%S”)
注:timedelta()的参数有:days,hours,seconds,microseconds

6.给定时间戳,计算该时间的几天前时间

复制代码代码如下:
timeStamp = 1381419600
#先转换为datetime
import datetime
import time
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray – datetime.timedelta(days = 3)
#参考5,可以转换为其他的任意格式了

7、用Python计算昨天和明天的日期

复制代码代码如下:
>>> import datetime #导入日期时间模块
>>> today = datetime.date.today() #获得今天的日期
>>> print today #输出今天日期
2014-01-04 
>>> yesterday = today – datetime.timedelta(days=1) #用今天日期减掉时间差,参数为1天,获得昨天的日期
>>> print yesterday
2014-01-03 
>>> tomorrow = today + datetime.timedelta(days=1) #用今天日期加上时间差,参数为1天,获得明天的日期
>>> print tomorrow
2014-01-05 
>>>
>>> print “昨天:%s, 今天:%s, 明天:%s” % (yesterday, today, tomorrow) #字符串拼接在一起输出,这3天的日期

昨天:2014-01-03, 今天:2014-01-04, 明天:2014-01-05
8、python里使用time模块来获取当前的时间

复制代码代码如下:

#!/usr/bin/python
import time
print (time.strftime(“%H:%M:%S”))
## 12 hour format ##
print (time.strftime(“%I:%M:%S”))
#:输出
#18:11:30
#6:11:30

9、打印出当前的日期的python程序

复制代码代码如下:
!/usr/bin/python

import time
## dd/mm/yyyy格式
print (time.strftime(“%d/%m/%Y”))

#输出:
11/03/2014

10、使用datetime模块来获取当前的日期和时间

复制代码代码如下:
#!/usr/bin/python
import datetime
i = datetime.datetime.now()
print (“当前的日期和时间是 %s” % i)
print (“ISO格式的日期和时间是 %s” % i.isoformat() )
print (“当前的年份是 %s” %i.year)
print (“当前的月份是 %s” %i.month)
print (“当前的日期是  %s” %i.day)
print (“dd/mm/yyyy 格式是  %s/%s/%s” % (i.day, i.month, i.year) )
print (“当前小时是 %s” %i.hour)
print (“当前分钟是 %s” %i.minute)
print (“当前秒是  %s” %i.second)

附:日期和时间的格式化参数

复制代码代码如下:

%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

转自:http://www.jb51.net/article/47957.htm

Spring Aop Advise方法(增强方法) 中获取目标方法的参数

 spring  Spring Aop Advise方法(增强方法) 中获取目标方法的参数已关闭评论
10月 272014
 

非常好的一篇文章,学习了:http://jackyin5918.iteye.com/blog/1918076

Spring Aop Advise方法(增强方法) 中获取目标方法的参数

 

1. 概念 

   

   切面类: 一种特殊bean,通过aop配置,其中的方法(增强方法),会对目标bean的目标方法做一些增强处理

   (比如在目标方法之前或之后调用等).

   

   切入点(pointcut): 一种规则,普通bean中符合这种规则的方法,将成为上面切面类中所说的目标方法,接受切面类方法

   的特殊处理.

   

   增强方法(Advice),包括aop:befor,aop:after,aop:after-retuing,aop:around,aop:throwing等.

   

2. 配置片段:

     

Xml代码  收藏代码

  1. <!– AOP测试的chinese –>  
  2.             <bean id=“chinese_aop” class=“test.aop.Chinese” />  
  3.             <!– 定义一个普通bean,作为切面bean –>  
  4.             <bean id=“accessArgAspect” class=“test.aop.aspect.AccessArgAspect” />  
  5.             <!– AOP配置  –>   
  6.             <aop:config>  
  7.               <!– 配置切面aspect –>  
  8.               <aop:aspect id=“aspect” ref=“accessArgAspect”>  
  9.                 <aop:after-returning   
  10.                   pointcut=“execution(* test.aop.*.*(..))”   
  11.                   method=“access”  
  12.                   returning=“retval”  
  13.                   arg-names=“time,food,retval”   
  14.                 />  
  15.               </aop:aspect>   
  16.             </aop:config>  
Xml代码  收藏代码

  1. <!– AOP测试的chinese –>  
  2.             <bean id=“chinese_aop” class=“test.aop.Chinese” />  
  3.             <!– 定义一个普通bean,作为切面bean –>  
  4.             <bean id=“accessArgAspect” class=“test.aop.aspect.AccessArgAspect” />  
  5.             <!– AOP配置  –>   
  6.             <aop:config>  
  7.               <!– 配置切面aspect –>  
  8.               <aop:aspect id=“aspect” ref=“accessArgAspect”>  
  9.                 <aop:after-returning   
  10.                   pointcut=“execution(* test.aop.*.*(..))”   
  11.                   method=“access”  
  12.                   returning=“retval”  
  13.                   arg-names=“time,food,retval”   
  14.                 />  
  15.               </aop:aspect>   
  16.             </aop:config>  

       

    上面的配置中:

    <bean id=”chinese_aop” class=”test.aop.Chinese” />

    配置了普通的bean,该bean中的一些方法

    即将满足切入点配置规则,接受切面类中增强方法(Advice)的增强处理.

    

    <bean id=”accessArgAspect” class=”test.aop.aspect.AccessArgAspect” /> 定义一个切面类,

    切面类可以是一个普通的bean.

    

    <aop:config>

      <!–配置切面aspect,通过ref关联到前面配置的作为切面类的bean–>

      <aop:aspect id=”aspect” ref=”accessArgAspect”>

        <!– 配置一种aop:after-returning增强处理–>

        <aop:after-returning

          <!– 切入点 规则,符合这个规则的普通bean中的方法将接受增强处理 –>

          pointcut=”execution(*test.aop.*.*(..)) and args(food,time,..)”  

          <!– 切面类中,作为增强处理的方法的名称 –>

          method=”access”

          <!– 普通bean,接受增强处理方法的返回值,void的类型被视为null –> 

          returning=”retval” 

          <!– 切面类中增强方法的参数名,这个配置可以省略 –>

          arg-names=”time,food,retval”

        />

        

      </aop:aspect>

    </aop:config>

    

3. 参数绑定

   切面类 方法(增强方法)获取 目标方法(普通bean中接受增强方法处理的方法) 参数的方式称为参数绑定.

   

   (1) 增强方法 不需要传递参数,则不需要参数绑定.

   

   (2) 增强方法中,只有一个JoinPoint类型的参数,使用这种参数,也不需要参数绑定.

       因为, JoinPoint类有一些方法可以获取目标方法的调用信息(包括参数信息),比如:

       Object[] getArgs(),返回在执行目标方法时传递的参数

       Signature getSignature(),获取 目标方法的签名

       Object getTarget():返回被植入 增强处理的目标对象

       Object getThis(): 返回AOP框架为目标对象生成的代理对象(Spring AOP通过动态代理实现)

       

       假如是aop:around类型的增强处理方法,可以使用ProceedingJoinPoint作为参数,

       ProceedingJoinPoint除了有上述的几个方法外,还有一个proceed()方法,替代目标方法执行.

       

   (3) 增强方法中,有普通类型的参数,

       比如public void access(Date time,String food, String retval)

       这种增强方法,必须要在pointcut中通过配置args 和 returning,保证参数正确绑定.

       (returning只针对aop:after-returning类型的增强处理,其他的可以省略)

       (即,增强方法中出现的参数名必须在pointcut配置中都得到明确的配置,否则报异常)

       pointcut=”execution(*test.aop.*.*(..)) and args(food,time,..)”

       中args(food,time,..)中的food和time是参数名,来自切面类的增强方法,不能乱写,

       必须和切面类中增强方法的参数名称一致,在切面类的增强方法参数列表中必须能找到.

       因为在增强方法public void access(Date time,String food, String retval)中,time是Date类型的,

       food是String类型的,所以pointcut(切入点)定义中and args(food,time,..)符合and前面的规则的同时

       还要符合args(food,time,..)的规则.

       args(food,time,..)表是所有 第一个参数是String类型,第二个参数是Date类型的方法才能称为目标方法.

       .. 表示可以有第三个,第四个, … , 第n个参数,但是至少有两个参数(String,Date)

       所以需要注意的有:

       a. 切面类中的增强方法参数,必须要在pointcut中有明确指定,比如

          public void access(Date time,String food, String retval)

          这个方法中三个参数food,和time通过args指定了,retval表示aop:after-returning定义的目标函数的返回值.

       b. 明确指定了参数后,AOP框架在运行时能正确绑定参数,因为:

          and args(food,time,..)表示 只对一类目标方法的调用做增强处理,这种目标方法是:在准备调用这种目标方法时,

          实际传递给它的参数为food和time所表示的类型(food和time必须在增强方法的参数列表中找到,这样就根据增强方法确定了food和time的类型).

          并且,传递参数的顺序也要是和args(food,time,..)中一致.这样在调用目标方法时,至少会按顺序传递food,time两个参数,

          AOP框架可以将这两个参数传递给 增强方法.

   

   (4) 增强方法中 有 JoinPoint和普通类型参数,

       则必须将JoinPoint类型的参数作为第一个参数,普通参数从第二个开始.

       其他的处理方式,按照上面(3)中仅含有普通参数的方式处理.

       

 主要代码;

Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  *  
  7.  * 普通bean的接口 
  8.  * 
  9.  */  
  10. public interface Person  
  11. {  
  12.   String sayHello(String name);  
  13.   void eat(String food, Date time);  
  14.   void eat2(String food, Date time,String test);  
  15. }  
Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  *  
  7.  * 普通bean的接口 
  8.  * 
  9.  */  
  10. public interface Person  
  11. {  
  12.   String sayHello(String name);  
  13.   void eat(String food, Date time);  
  14.   void eat2(String food, Date time,String test);  
  15. }  

 

Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  *  
  7.  *一个普通bean,eat方法和sayHello方法,是需要被切入,动态影响的 
  8.  * 
  9.  */  
  10. public class Chinese implements Person  
  11. {  
  12.   
  13.   @Override  
  14.   public void eat(String afood, Date atime)  
  15.   {  
  16.     System.out.println(“正在吃: “ + afood + “, 时间是: “ + atime);  
  17.   }  
  18.   
  19.   @Override  
  20.   public String sayHello(String name)  
  21.   {  
  22.     return name + ” Hello, Spring AOP.”;  
  23.   }  
  24.     
  25.   public void eat2(String afood, Date atime,String test)  
  26.   {  
  27.     System.out.println(“eat2 ——— 正在吃: “ + afood + “, 时间是: “ + atime + “, eat2里面的test= “ + test);  
  28.   }  
  29.   
  30. }  
Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. /** 
  6.  *  
  7.  *一个普通bean,eat方法和sayHello方法,是需要被切入,动态影响的 
  8.  * 
  9.  */  
  10. public class Chinese implements Person  
  11. {  
  12.   
  13.   @Override  
  14.   public void eat(String afood, Date atime)  
  15.   {  
  16.     System.out.println(“正在吃: “ + afood + “, 时间是: “ + atime);  
  17.   }  
  18.   
  19.   @Override  
  20.   public String sayHello(String name)  
  21.   {  
  22.     return name + ” Hello, Spring AOP.”;  
  23.   }  
  24.     
  25.   public void eat2(String afood, Date atime,String test)  
  26.   {  
  27.     System.out.println(“eat2 ——— 正在吃: “ + afood + “, 时间是: “ + atime + “, eat2里面的test= “ + test);  
  28.   }  
  29.   
  30. }  

 

Java代码  收藏代码

  1. package test.aop.aspect;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.Date;  
  5.   
  6. import org.aspectj.lang.JoinPoint;  
  7.   
  8. /** 
  9.  *  
  10.  * 定义切面类,处理test.aop.Chinese.eat() 
  11.  * 
  12.  */  
  13. public class AccessArgAspect  
  14. {  
  15.     
  16.   //普通的 增强方法  
  17.   public void access(Date time,String food, String retval)  
  18.   {  
  19.     System.out.println(“”);  
  20.       
  21.     System.out.println(“目标方法中 String 参数为: “ + food);  
  22.     System.out.println(“目标方法中 String 参数为: “ + time);  
  23.     System.out.println(“目标方法 返回值: “ + retval);  
  24.       
  25.     System.out.println(“模拟记录日志…”);  
  26.       
  27.   }  
  28.     
  29.   /** 
  30.    * 第一个参数为JoinPoint类型的增强方法,JoinPoint必须为第一个参数 
  31.    * @param jp 
  32.    * @param time 
  33.    * @param food 
  34.    * @param retval 
  35.    */  
  36.   public void accessWithJoinPoint(JoinPoint jp,Date time,String food)  
  37.   {  
  38.     System.out.println(“”);  
  39.     System.out.println(“JoinPoint.getArgs()获取参数列表:” + Arrays.toString(jp.getArgs()));  
  40.       
  41.     System.out.println(“JoinPoint jp —- 目标方法中 String 参数为: “ + food);  
  42.     System.out.println(“JoinPoint jp —- 目标方法中 String 参数为: “ + time);  
  43.       
  44.     System.out.println(“JoinPoint jp —- 模拟记录日志…”);  
  45.       
  46.   }  
  47. }  
Java代码  收藏代码

  1. package test.aop.aspect;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.Date;  
  5.   
  6. import org.aspectj.lang.JoinPoint;  
  7.   
  8. /** 
  9.  *  
  10.  * 定义切面类,处理test.aop.Chinese.eat() 
  11.  * 
  12.  */  
  13. public class AccessArgAspect  
  14. {  
  15.     
  16.   //普通的 增强方法  
  17.   public void access(Date time,String food, String retval)  
  18.   {  
  19.     System.out.println(“”);  
  20.       
  21.     System.out.println(“目标方法中 String 参数为: “ + food);  
  22.     System.out.println(“目标方法中 String 参数为: “ + time);  
  23.     System.out.println(“目标方法 返回值: “ + retval);  
  24.       
  25.     System.out.println(“模拟记录日志…”);  
  26.       
  27.   }  
  28.     
  29.   /** 
  30.    * 第一个参数为JoinPoint类型的增强方法,JoinPoint必须为第一个参数 
  31.    * @param jp 
  32.    * @param time 
  33.    * @param food 
  34.    * @param retval 
  35.    */  
  36.   public void accessWithJoinPoint(JoinPoint jp,Date time,String food)  
  37.   {  
  38.     System.out.println(“”);  
  39.     System.out.println(“JoinPoint.getArgs()获取参数列表:” + Arrays.toString(jp.getArgs()));  
  40.       
  41.     System.out.println(“JoinPoint jp —- 目标方法中 String 参数为: “ + food);  
  42.     System.out.println(“JoinPoint jp —- 目标方法中 String 参数为: “ + time);  
  43.       
  44.     System.out.println(“JoinPoint jp —- 模拟记录日志…”);  
  45.       
  46.   }  
  47. }  

 

Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. /** 
  9.  *  
  10.  * AOP客户端测试类 
  11.  * 
  12.  */  
  13. public class TestClient  
  14. {  
  15.   public static void main(String[] args)  
  16.   {  
  17.     ApplicationContext ctx = new ClassPathXmlApplicationContext(“bean.xml”);  
  18.     System.out.println(ctx);  
  19.       
  20.     Person person = ctx.getBean(“chinese_aop”,Person.class);  
  21.       
  22.     person.sayHello(“jack”);  
  23.       
  24.     person.eat(“米饭”,new Date());  
  25.       
  26.     person.eat2(“米饭”,new Date(),“test”);  
  27.   }  
  28. }  
Java代码  收藏代码

  1. package test.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. /** 
  9.  *  
  10.  * AOP客户端测试类 
  11.  * 
  12.  */  
  13. public class TestClient  
  14. {  
  15.   public static void main(String[] args)  
  16.   {  
  17.     ApplicationContext ctx = new ClassPathXmlApplicationContext(“bean.xml”);  
  18.     System.out.println(ctx);  
  19.       
  20.     Person person = ctx.getBean(“chinese_aop”,Person.class);  
  21.       
  22.     person.sayHello(“jack”);  
  23.       
  24.     person.eat(“米饭”,new Date());  
  25.       
  26.     person.eat2(“米饭”,new Date(),“test”);  
  27.   }  
  28. }  

 

Xml代码  收藏代码

  1. <?xml version=“1.0” encoding=“UTF-8”?>    
  2. <beans xmlns=“http://www.springframework.org/schema/beans”    
  3.         xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”    
  4.         xmlns:aop=“http://www.springframework.org/schema/aop”  
  5.         xsi:schemaLocation=”http://www.springframework.org/schema/beans    
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/aop   
  8.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9.             http://www.springframework.org/schema/tx   
  10.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>   
  11.               
  12.             <!– 小试牛刀 property使用 name,value–>  
  13.             <bean id=“personService” class=“test.spring.PersonService”>  
  14.               <property name=“name” value=“wawa”></property>  
  15.             </bean>  
  16.               
  17.             <!–   
  18.                   设值注入  
  19.                 property使用 name,ref ,  
  20.                 ref也是一个bean的id,ref 可以在stoneAxe和steelAxe之间随意切换,而不用修改java代码   
  21.             —>  
  22.             <bean id=“chinese” class=“test.ioc.setter.Chinese”>  
  23.               <property name=“axe” ref=“steelAxe”></property>  
  24.             </bean>  
  25.             <bean id=“stoneAxe” class=“test.ioc.setter.StoneAxe” />   
  26.             <bean id=“steelAxe” class=“test.ioc.setter.SteelAxe” />   
  27.               
  28.             <!–   
  29.                 构造注入  
  30.                 constructor-arg标签,ref ,  
  31.                 ref也是一个bean的id,ref 可以在stoneAxe和steelAxe之间随意切换,而不用修改java代码  
  32.                 constructor-arg 也可以配置value,表示传递给构造函数的是一个 普通的值,而不是另一个bean   
  33.             —>  
  34.             <bean id=“chinese1” class=“test.ioc.constructor.Chinese”>  
  35.               <constructor-arg ref=“steelAxe” />   
  36.             </bean>  
  37.               
  38.             <!– 国际化 –>  
  39.            <bean id=“messageSource” class=“org.springframework.context.support.ResourceBundleMessageSource”>  
  40.               <property name=“basenames” >  
  41.                 <list>  
  42.                   <!– 这里的value可带路径   –>  
  43.                   <value>message</value>  
  44.                 </list>  
  45.               </property>   
  46.             </bean>  
  47.               
  48.             <!–   
  49.               ApplicationContext的事件机制   
  50.                 在Spring中配置了实现ApplicationListener接口的Bean,  
  51.               Spring容器就会把这个Bean当成容器事件的监听器  
  52.             —>  
  53.             <bean class=“test.springevent.EmainNotifier” />   
  54.               
  55.             <!– bean中获取 ApplicationContext引用–>  
  56.             <bean name=“beangetappcontext” class=“test.bean.get.appcontext.BeanGetAppContext” />  
  57.               
  58.             <!– AOP测试的chinese –>  
  59.             <bean id=“chinese_aop” class=“test.aop.Chinese” />  
  60.             <!– 定义一个普通bean,作为切面bean –>  
  61.             <bean id=“accessArgAspect” class=“test.aop.aspect.AccessArgAspect” />  
  62.             <!– AOP配置  –>   
  63.             <aop:config>  
  64.               <!– 配置切面aspect –>  
  65.               <aop:aspect id=“aspect” ref=“accessArgAspect”>  
  66.                 <aop:after-returning   
  67.                   pointcut=“execution(* test.aop.*.*(..)) and args(food,time,..)”   
  68.                   method=“access”  
  69.                   returning=“retval”  
  70.                   arg-names=“time,food,retval”   
  71.                 />  
  72.                   
  73.                 <aop:before   
  74.                   pointcut=“execution(* test.aop.*.*(..)) and args(food,time,..)”   
  75.                   method=“accessWithJoinPoint”  
  76.                 />  
  77.                   
  78.               </aop:aspect>   
  79.             </aop:config>  
  80.                
  81. </beans>   
Xml代码  收藏代码

  1. <?xml version=“1.0” encoding=“UTF-8”?>    
  2. <beans xmlns=“http://www.springframework.org/schema/beans”    
  3.         xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”    
  4.         xmlns:aop=“http://www.springframework.org/schema/aop”  
  5.         xsi:schemaLocation=”http://www.springframework.org/schema/beans    
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/aop   
  8.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9.             http://www.springframework.org/schema/tx   
  10.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>   
  11.               
  12.             <!– 小试牛刀 property使用 name,value–>  
  13.             <bean id=“personService” class=“test.spring.PersonService”>  
  14.               <property name=“name” value=“wawa”></property>  
  15.             </bean>  
  16.               
  17.             <!–   
  18.                   设值注入  
  19.                 property使用 name,ref ,  
  20.                 ref也是一个bean的id,ref 可以在stoneAxe和steelAxe之间随意切换,而不用修改java代码   
  21.             —>  
  22.             <bean id=“chinese” class=“test.ioc.setter.Chinese”>  
  23.               <property name=“axe” ref=“steelAxe”></property>  
  24.             </bean>  
  25.             <bean id=“stoneAxe” class=“test.ioc.setter.StoneAxe” />   
  26.             <bean id=“steelAxe” class=“test.ioc.setter.SteelAxe” />   
  27.               
  28.             <!–   
  29.                 构造注入  
  30.                 constructor-arg标签,ref ,  
  31.                 ref也是一个bean的id,ref 可以在stoneAxe和steelAxe之间随意切换,而不用修改java代码  
  32.                 constructor-arg 也可以配置value,表示传递给构造函数的是一个 普通的值,而不是另一个bean   
  33.             —>  
  34.             <bean id=“chinese1” class=“test.ioc.constructor.Chinese”>  
  35.               <constructor-arg ref=“steelAxe” />   
  36.             </bean>  
  37.               
  38.             <!– 国际化 –>  
  39.            <bean id=“messageSource” class=“org.springframework.context.support.ResourceBundleMessageSource”>  
  40.               <property name=“basenames” >  
  41.                 <list>  
  42.                   <!– 这里的value可带路径   –>  
  43.                   <value>message</value>  
  44.                 </list>  
  45.               </property>   
  46.             </bean>  
  47.               
  48.             <!–   
  49.               ApplicationContext的事件机制   
  50.                 在Spring中配置了实现ApplicationListener接口的Bean,  
  51.               Spring容器就会把这个Bean当成容器事件的监听器  
  52.             —>  
  53.             <bean class=“test.springevent.EmainNotifier” />   
  54.               
  55.             <!– bean中获取 ApplicationContext引用–>  
  56.             <bean name=“beangetappcontext” class=“test.bean.get.appcontext.BeanGetAppContext” />  
  57.               
  58.             <!– AOP测试的chinese –>  
  59.             <bean id=“chinese_aop” class=“test.aop.Chinese” />  
  60.             <!– 定义一个普通bean,作为切面bean –>  
  61.             <bean id=“accessArgAspect” class=“test.aop.aspect.AccessArgAspect” />  
  62.             <!– AOP配置  –>   
  63.             <aop:config>  
  64.               <!– 配置切面aspect –>  
  65.               <aop:aspect id=“aspect” ref=“accessArgAspect”>  
  66.                 <aop:after-returning   
  67.                   pointcut=“execution(* test.aop.*.*(..)) and args(food,time,..)”   
  68.                   method=“access”  
  69.                   returning=“retval”  
  70.                   arg-names=“time,food,retval”   
  71.                 />  
  72.                   
  73.                 <aop:before   
  74.                   pointcut=“execution(* test.aop.*.*(..)) and args(food,time,..)”   
  75.                   method=“accessWithJoinPoint”  
  76.                 />  
  77.                   
  78.               </aop:aspect>   
  79.             </aop:config>  
  80.                
  81. </beans>   

 

工程文件注:

 

使用Spring 3.2.0的所有ja包,

3.2.0的srping包,做AOP需要依赖(Spring 3.2.0中去除了依赖包,需要自己找)

com.springsource.org.aopalliance-1.0.0.jar,下载地址http://ebr.springsource.com/repository/app/bundle/version/detail?name=org.springframework.aop&version=3.2.0.RELEASE

还有,3.0.2的com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar包,

下载3.0.2版本的依赖包以后,找

spring-framework-3.0.2.RELEASE-dependencies (1)org.aspectjcom.springsource.org.aspectj.weaver1.6.8.RELEASE这个目录里面有